wp_date( string $format, int $timestamp = null, DateTimeZone $timezone = null ): string|false
- Since
- 5.3.0
- Source
wp-includes/functions.php:244
Description
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.
Compatibility
- WordPress
- since 5.3.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$formatstring- PHP date format.
$timestampintoptional- Unix timestamp. Defaults to current time.Default:
null $timezoneDateTimeZoneoptional- Timezone to output result in. Defaults to timezone from site settings.Default:
null
Return value
string|false- The date, translated if locale specifies it. False on invalid timestamp input.
Performance profile
How much work a call to wp_date() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 9–66
- Plugin surface
- 1 hook
- Called by
- 6
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 157.
Third-party callbacks on 'wp_date' run inside this call, and their cost is not bounded by anything here.
6 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_date() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$timestamp !== null && !$timestamp | 9 | none |
$timestamp !== null && $timestamp && empty($wp_locale) | 33–35 | date_create(), ->setTimezone(), ->format(), apply_filters() |
$timestamp === null && empty($wp_locale) | 34–36 | time(), date_create(), ->setTimezone(), ->format(), apply_filters() |
$timestamp !== null && $timestamp && empty($wp_locale) | 36–38 | wp_timezone(), date_create(), ->setTimezone(), ->format(), apply_filters() |
$timestamp === null && empty($wp_locale) | 37–39 | time(), wp_timezone(), date_create(), ->setTimezone(), ->format(), apply_filters() |
$timestamp !== null && $timestamp && !empty($wp_locale) && !$i | 62 | date_create(), ->setTimezone(), ->format(), ->get_month(), ->format(), ->get_weekday(), ->format(), wp_maybe_decline_date(), apply_filters() |
$timestamp === null && !empty($wp_locale) && !$i | 63 | time(), date_create(), ->setTimezone(), ->format(), ->get_month(), ->format(), ->get_weekday(), ->format(), wp_maybe_decline_date(), apply_filters() |
$timestamp !== null && $timestamp && !empty($wp_locale) && !$i | 65 | wp_timezone(), date_create(), ->setTimezone(), ->format(), ->get_month(), ->format(), ->get_weekday(), ->format(), wp_maybe_decline_date(), apply_filters() |
$timestamp === null && !empty($wp_locale) && !$i | 66 | time(), wp_timezone(), date_create(), ->setTimezone(), ->format(), ->get_month(), ->format(), ->get_weekday(), ->format(), wp_maybe_decline_date(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 157 | 9–66 | 15 | |
| 8.5 | 157 | 9–66 | 15 | |
| 8.4 | 157 | 9–66 | 15 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 162 | 11–70 | 15 | |
| 8.2 | 162 | 11–70 | 15 | 1 more instruction than PHP 8.1 |
| 8.1 | 161 | 11–70 | 15 | |
| 7.4 | 161 | 11–70 | 15 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while wp_date() runs, in this order:
- apply_filters( wp_date )filterline 319 (+75 into the body)
Filters the date formatted based on the locale.
Uses · 3
- wp_timezone()Retrieves the timezone of the site as a `DateTimeZone` object.
- wp_maybe_decline_date()Determines if the date should be declined.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 6
- WP_Debug_Data::get_wp_server()Gets the WordPress server section of the debug data.
- WP_Sitemaps_Posts::get_url_list()Gets a URL list for a post type sitemap.
- date_i18n()Retrieves the date in localized format, based on a sum of Unix timestamp and timezone offset in seconds.
- get_post_modified_time()Retrieves the time at which the post was last modified.
- get_post_time()Retrieves the localized time of the post.
- mysql2date()Converts given MySQL date string into a different format.
Source code
function wp_date( $format, $timestamp = null, $timezone = null ) { global $wp_locale; if ( null === $timestamp ) { $timestamp = time(); } elseif ( ! is_numeric( $timestamp ) ) { return false; } if ( ! $timezone ) { $timezone = wp_timezone(); } $datetime = date_create( '@' . $timestamp ); $datetime->setTimezone( $timezone ); if ( empty( $wp_locale->month ) || empty( $wp_locale->weekday ) ) { $date = $datetime->format( $format ); } else { // We need to unpack shorthand `r` format because it has parts that might be localized. $format = preg_replace( '/(?<!\\\\)r/', DATE_RFC2822, $format ); $new_format = ''; $format_length = strlen( $format ); $month = $wp_locale->get_month( $datetime->format( 'm' ) ); $weekday = $wp_locale->get_weekday( $datetime->format( 'w' ) ); for ( $i = 0; $i < $format_length; $i++ ) { switch ( $format[ $i ] ) { case 'D': $new_format .= addcslashes( $wp_locale->get_weekday_abbrev( $weekday ), '\\A..Za..z' ); break; case 'F': $new_format .= addcslashes( $month, '\\A..Za..z' ); break; case 'l': $new_format .= addcslashes( $weekday, '\\A..Za..z' ); break; case 'M': $new_format .= addcslashes( $wp_locale->get_month_abbrev( $month ), '\\A..Za..z' ); break; case 'a': $new_format .= addcslashes( $wp_locale->get_meridiem( $datetime->format( 'a' ) ), '\\A..Za..z' ); break; case 'A': $new_format .= addcslashes( $wp_locale->get_meridiem( $datetime->format( 'A' ) ), '\\A..Za..z' ); break; case '\\': $new_format .= $format[ $i ]; // If character follows a slash, we add it without translating. if ( $i < $format_length ) { $new_format .= $format[ ++$i ]; } break; default: $new_format .= $format[ $i ]; break; } } $date = $datetime->format( $new_format ); $date = wp_maybe_decline_date( $date, $format ); } /** * Filters the date formatted based on the locale. * * @since 5.3.0 * * @param string $date Formatted date string. * @param string $format Format to display the date. * @param int $timestamp Unix timestamp. * @param DateTimeZone $timezone Timezone. */ $date = apply_filters( 'wp_date', $date, $format, $timestamp, $timezone ); return $date;}Changelog
Introduced in 5.3.0. 2 changes between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$timestamp retyped from int to int|null.verified against source$timezone retyped from DateTimeZone to DateTimeZone|null.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.