wppaste
WordPress

wp_date( string $format, int $timestamp = null, DateTimeZone $timezone = null ): string|false

Since
5.3.0
Source
wp-includes/functions.php:239
Retrieves the date, in localized format.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
9–66

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 157.

Plugin surface
1 hook

Third-party callbacks on 'wp_date' run inside this call, and their cost is not bounded by anything here.

Called by
6

6 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
$timestamp !== null && !$timestamp9none
$timestamp !== null && $timestamp && empty($wp_locale)33–35date_create(), ->setTimezone(), ->format(), apply_filters()
$timestamp === null && empty($wp_locale)34–36time(), date_create(), ->setTimezone(), ->format(), apply_filters()
$timestamp !== null && $timestamp && empty($wp_locale)36–38wp_timezone(), date_create(), ->setTimezone(), ->format(), apply_filters()
$timestamp === null && empty($wp_locale)37–39time(), wp_timezone(), date_create(), ->setTimezone(), ->format(), apply_filters()
$timestamp !== null && $timestamp && !empty($wp_locale) && !$i62date_create(), ->setTimezone(), ->format(), ->get_month(), ->format(), ->get_weekday(), ->format(), wp_maybe_decline_date(), apply_filters()
$timestamp === null && !empty($wp_locale) && !$i63time(), date_create(), ->setTimezone(), ->format(), ->get_month(), ->format(), ->get_weekday(), ->format(), wp_maybe_decline_date(), apply_filters()
$timestamp !== null && $timestamp && !empty($wp_locale) && !$i65wp_timezone(), date_create(), ->setTimezone(), ->format(), ->get_month(), ->format(), ->get_weekday(), ->format(), wp_maybe_decline_date(), apply_filters()
$timestamp === null && !empty($wp_locale) && !$i66time(), wp_timezone(), date_create(), ->setTimezone(), ->format(), ->get_month(), ->format(), ->get_weekday(), ->format(), wp_maybe_decline_date(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1579–6615
8.51579–6615
8.41579–66155 fewer instructions than PHP 8.3
8.316211–7015
8.216211–70151 more instruction than PHP 8.1
8.116111–7015
7.416111–7015

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:

  1. apply_filters( wp_date )filterline 314 (+75 into the body)

    Filters the date formatted based on the locale.

Uses · 3

Used by · 6

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.9.7
Parameter $timestamp retyped from int to int|null.verified against source
6.9.7
Parameter $timezone retyped from DateTimeZone to DateTimeZone|null.verified against source
5.3.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 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.