wp_date()
Retrieves the date, in localized format.
This is a newer function, intended to replace date_i18n() without legacy quirks in it.
Note that, unlike date_i18n(), this function accepts a true Unix timestamp, not summed with timezone offset.
Uses: WP_Locale(), wp_timezone()
Used By: date_i18n(), mysql2date()
Hooks from the function
Return
String|false
. The date, translated if locale specifies it. False on invalid timestamp input.
Usage
wp_date( $format, $timestamp, $timezone );
- $format(string) (required)
- PHP date format.
- $timestamp(int)
- Unix timestamp.
Default: current time - $timezone(DateTimeZone)
- Timezone to output result in.
Default: timezone from site settings
Examples
#1 Localization of the php function date() in WordPress
Current time:
$format = 'j F Y H:i:s'; echo wp_date( $format ); // March 24, 2021 08:12:07 (website time = +5 hours) echo date_i18n( $format ); // March 24, 2021 08:12:07 (website time = +5 hours) echo date( $format ); // March 24, 2021 03:12:07 (UTC time)
Specified time:
echo wp_date( 'j F Y H:i:s', 0 ); // January 1, 1970 06:00:00 echo wp_date( 'j M Y H:i:s', strtotime('1999-11-15') ); // 15 Nov 1999 05:00:00 (site time = +5 hours) echo wp_date( 'j M Y H:i:s', strtotime('1999-11-15'), new DateTimeZone('UTC') ); // 15 Nov 1999 00:00:00 (UTC time)
#2 Get a translatable the_date() equivalent
To do this you will need:
echo wp_date( get_option( 'date_format' ), get_post_timestamp() );
Useful information: https://make.wordpress.org/core/2019/09/23/date-time-improvements-wp-5-3/
Notes
- Global. WP_Locale. $wp_locale WordPress date and time locale object.
Changelog
Since 5.3.0 | Introduced. |