W3cubDocs

/WordPress

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

Retrieve 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.

Parameters

$dateformatstring

(string) (Required) Format to display the date.

$timestamp_with_offset

(int|bool) (Optional) A sum of Unix timestamp and timezone offset in seconds.

Default value: false

$gmt

(bool) (Optional) Whether to use GMT timezone. Only applies if timestamp is not provided.

Default value: false

Return

(string) The date, translated if locale specifies it.

Source

File: wp-includes/functions.php

function date_i18n( $dateformatstring, $timestamp_with_offset = false, $gmt = false ) {
	global $wp_locale;
	$i = $timestamp_with_offset;

	if ( false === $i ) {
		$i = current_time( 'timestamp', $gmt );
	}

	/*
	 * Store original value for language with untypical grammars.
	 * See https://core.trac.wordpress.org/ticket/9396
	 */
	$req_format = $dateformatstring;

	$dateformatstring = preg_replace( '/(?<!\\\\)c/', DATE_W3C, $dateformatstring );
	$dateformatstring = preg_replace( '/(?<!\\\\)r/', DATE_RFC2822, $dateformatstring );

	if ( ( ! empty( $wp_locale->month ) ) && ( ! empty( $wp_locale->weekday ) ) ) {
		$datemonth            = $wp_locale->get_month( date( 'm', $i ) );
		$datemonth_abbrev     = $wp_locale->get_month_abbrev( $datemonth );
		$dateweekday          = $wp_locale->get_weekday( date( 'w', $i ) );
		$dateweekday_abbrev   = $wp_locale->get_weekday_abbrev( $dateweekday );
		$datemeridiem         = $wp_locale->get_meridiem( date( 'a', $i ) );
		$datemeridiem_capital = $wp_locale->get_meridiem( date( 'A', $i ) );
		$dateformatstring     = ' ' . $dateformatstring;
		$dateformatstring     = preg_replace( '/([^\\\])D/', "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
		$dateformatstring     = preg_replace( '/([^\\\])F/', "\\1" . backslashit( $datemonth ), $dateformatstring );
		$dateformatstring     = preg_replace( '/([^\\\])l/', "\\1" . backslashit( $dateweekday ), $dateformatstring );
		$dateformatstring     = preg_replace( '/([^\\\])M/', "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
		$dateformatstring     = preg_replace( '/([^\\\])a/', "\\1" . backslashit( $datemeridiem ), $dateformatstring );
		$dateformatstring     = preg_replace( '/([^\\\])A/', "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );

		$dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) - 1 );
	}
	$timezone_formats    = array( 'P', 'I', 'O', 'T', 'Z', 'e' );
	$timezone_formats_re = implode( '|', $timezone_formats );
	if ( preg_match( "/$timezone_formats_re/", $dateformatstring ) ) {
		$timezone_string = get_option( 'timezone_string' );
		if ( false === $timestamp_with_offset && $gmt ) {
			$timezone_string = 'UTC';
		}
		if ( $timezone_string ) {
			$timezone_object = timezone_open( $timezone_string );
			$date_object     = date_create( null, $timezone_object );
			foreach ( $timezone_formats as $timezone_format ) {
				if ( false !== strpos( $dateformatstring, $timezone_format ) ) {
					$formatted        = date_format( $date_object, $timezone_format );
					$dateformatstring = ' ' . $dateformatstring;
					$dateformatstring = preg_replace( "/([^\\\])$timezone_format/", "\\1" . backslashit( $formatted ), $dateformatstring );
					$dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) - 1 );
				}
			}
		} else {
			$offset = get_option( 'gmt_offset' );
			foreach ( $timezone_formats as $timezone_format ) {
				if ( 'I' === $timezone_format ) {
					continue;
				}

				if ( false !== strpos( $dateformatstring, $timezone_format ) ) {
					if ( 'Z' === $timezone_format ) {
						$formatted = (string) ( $offset * HOUR_IN_SECONDS );
					} else {
						$prefix    = '';
						$hours     = (int) $offset;
						$separator = '';
						$minutes   = abs( ( $offset - $hours ) * 60 );

						if ( 'T' === $timezone_format ) {
							$prefix = 'GMT';
						} elseif ( 'e' === $timezone_format || 'P' === $timezone_format ) {
							$separator = ':';
						}

						$formatted = sprintf( '%s%+03d%s%02d', $prefix, $hours, $separator, $minutes );
					}

					$dateformatstring = ' ' . $dateformatstring;
					$dateformatstring = preg_replace( "/([^\\\])$timezone_format/", "\\1" . backslashit( $formatted ), $dateformatstring );
					$dateformatstring = substr( $dateformatstring, 1 );
				}
			}
		}
	}
	$j = @date( $dateformatstring, $i );

	/**
	 * Filters the date formatted based on the locale.
	 *
	 * @since 2.8.0
	 *
	 * @param string $j          Formatted date string.
	 * @param string $req_format Format to display the date.
	 * @param int    $i          A sum of Unix timestamp and timezone offset in seconds.
	 * @param bool   $gmt        Whether to use GMT timezone. Only applies if timestamp was
	 *                           not provided. Default false.
	 */
	$j = apply_filters( 'date_i18n', $j, $req_format, $i, $gmt );
	return $j;
}

Changelog

Version Description
0.71 Introduced.

© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/date_i18n