wp_date()
Gets the localized date (translated into the current language) from the given Unix timestamp.
This function is a wrapper for the PHP function date().
If the output date uses a format with the month or week as a string, the function will attempt to translate them into the current language. In other cases, it is analogous to date() in PHP.
This is a new function intended to replace date_i18n().
Note that this function returns the date, not outputs it to the screen.
This function, unlike date_i18n(), requires a Unix timestamp to be passed without adding the site's timezone offset.
The timezone offset is added automatically within the function based on the third parameter $timezone, where an arbitrary timezone offset can be specified.
In connection with the introduction of a new API for working with time in version 5.3:
Not recommended:
-
DO NOT retrieve time as a "WP timestamps" (timestamp + site time offset):
- current_time( 'timestamp' ) or
current_time( 'U' ) - get_post_time( 'U' )
- current_time( 'timestamp' ) or
-
DO NOT localize time/date using the function:
-
DO NOT persistently store "WP timestamps".
-
DO NOT compare "WP timestamps".
- DO NOT change the PHP time zone via date_default_timezone_set() - this is an important requirement for the core to work correctly. By default, WP sets
date_default_timezone_set( 'UTC' )and this setting should never be changed!
Recommended:
Below are the DATE_RFC3339 formats.
-
Retrieve time as a "Unix timestamp" (Unix timestamp without offset) or as a DateTimeImmutable object:
-
Localize time/date based on Unix timestamp:
- wp_date( DATE_RFC3339 ) - this is the same as wp_date( DATE_RFC3339, time() ).
-
Store "Unix timestamps" or formats that accurately specify the moment of time, such as DATE_RFC3339.
-
Compare Unix timestamps, DateTimeImmutable objects, or string dates in a single time zone.
- Use objects: DateTimeZone and DateTimeImmutable with the wp_date() function when processing time in a different time zone from the site's zone.
Also read: Date/Time in WordPress.
Hooks from the function
Returns
String|false. The translated date. False when an incorrect timestamp is provided.
Usage
wp_date( $format, $timestamp, $timezone );
- $format(string) (required)
- The format in which to output the date, example:
F j, Y- November 6, 2010. Full list of formats. The format can use the option get_option( 'date_format' ), which is set in the "Settings" section. - $timestamp(number)
- The Unix timestamp from which the date will be obtained in the specified format. By default, the current timestamp time() is used.
Default: null - (time() - current UNIX time) - $timezone(DateTimeZone)
- The timezone that will be applied to the specified timestamp. Default: wp_timezone().
Default: null (current timezone from site options)
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. |