wppaste
WordPress

date_i18n( string $format, int|bool $timestamp_with_offset = false, bool $gmt = false ): string

Since
0.71, 5.3.0
Source
wp-includes/functions.php:179
Retrieves the date in localized format, based on a sum of Unix timestamp and timezone offset in seconds.

Description

If the locale specifies the locale month and weekday, then the locale will take over the format for the date. If it isn't, then the date format string will be used instead. Note that due to the way WP typically generates a sum of timestamp and offset with strtotime(), it implies offset added at a <em>current</em> time, not at the time the timestamp represents. Storing such timestamps or calculating them differently will lead to invalid output.

Parameters

$formatstring
Format to display the date.
$timestamp_with_offsetint|booloptional
A sum of Unix timestamp and timezone offset in seconds. Default false.Default: false
$gmtbooloptional
Whether to use GMT timezone. Only applies if timestamp is not provided. Default false.Default: false

Return

string
The date, translated if locale specifies it.

Hooks fired · 1

One hook fires while date_i18n() runs, in this order:

  1. apply_filters( date_i18n )filterline 220 (+41 into the body)

    Filters the date formatted based on the locale.

Uses · 5

  • current_time()Retrieves the current time based on specified type.
  • wp_date()Retrieves the date, in localized format.
  • wp_timezone()Retrieves the timezone of the site as a `DateTimeZone` object.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • DateTimeZone::__construct()

Used by · 21

Show all 21

Source

function date_i18n( $format, $timestamp_with_offset = false, $gmt = false ) {	$timestamp = $timestamp_with_offset; 	// If timestamp is omitted it should be current time (summed with offset, unless `$gmt` is true).	if ( ! is_numeric( $timestamp ) ) {		// phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested		$timestamp = current_time( 'timestamp', $gmt );	} 	/*	 * This is a legacy implementation quirk that the returned timestamp is also with offset.	 * Ideally this function should never be used to produce a timestamp.	 */	if ( 'U' === $format ) {		$date = $timestamp;	} elseif ( $gmt && false === $timestamp_with_offset ) { // Current time in UTC.		$date = wp_date( $format, null, new DateTimeZone( 'UTC' ) );	} elseif ( false === $timestamp_with_offset ) { // Current time in site's timezone.		$date = wp_date( $format );	} else {		/*		 * Timestamp with offset is typically produced by a UTC `strtotime()` call on an input without timezone.		 * This is the best attempt to reverse that operation into a local time to use.		 */		$local_time = gmdate( 'Y-m-d H:i:s', $timestamp );		$timezone   = wp_timezone();		$datetime   = date_create( $local_time, $timezone );		$date       = wp_date( $format, $datetime->getTimestamp(), $timezone );	} 	/**	 * Filters the date formatted based on the locale.	 *	 * @since 2.8.0	 *	 * @param string $date      Formatted date string.	 * @param string $format    Format to display the date.	 * @param int    $timestamp A sum of Unix timestamp and timezone offset in seconds.	 *                          Might be without offset if input omitted timestamp but requested GMT.	 * @param bool   $gmt       Whether to use GMT timezone. Only applies if timestamp was not provided.	 */	$date = apply_filters( 'date_i18n', $date, $format, $timestamp, $gmt ); 	return $date;}

History

Introduced in 0.71. Unchanged from 6.7.7 through 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.

5.3.0
Converted into a wrapper for wp_date().from the docblock
0.71
Introduced.from the docblock

About this page

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