date_i18n()函数是Wordpress函数,根据Unix时间戳和时区偏移量(秒)的总和,以本地化格式检索日期。
date_i18n( string $format, int|bool $timestamp_with_offset = false, bool $gmt = false )
说明(Description)
如果区域设置指定区域设置月份和工作日,则区域设置将接管日期的格式。如果不是,则改用日期格式字符串。
请注意,由于WP通常使用strtotime()生成时间戳和偏移量之和的方式,它意味着在当前时间添加偏移量,而不是在时间戳表示的时间添加偏移量。存储这些时间戳或以不同方式计算它们将导致无效输出。
参数(Parameters)
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
$format | (string) | 必需 | 显示日期的格式。 |
$timestamp_with_offset | (int | bool) | 可选 | Unix时间戳和时区偏移量的总和(秒)。 |
$gmt | (bool) | 可选 | 是否使用GMT时区。仅适用于未提供时间戳的情况。 |
返回(Return)
(string)日期,如果语言环境指定,则转换为该日期。
源码(Source)
/**
* Retrieve the date in localized format, based on timestamp.
*
* 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.
*
* @since 0.71
*
* @global WP_Locale $wp_locale
*
* @param string $dateformatstring Format to display the date.
* @param bool|int $unixtimestamp Optional. Unix timestamp. Default false.
* @param bool $gmt Optional. Whether to use GMT timezone. Default false.
*
* @return string The date, translated if locale specifies it.
*/
function date_i18n( $dateformatstring, $unixtimestamp = false, $gmt = false ) {
global $wp_locale;
$i = $unixtimestamp;
if ( false === $i ) {
if ( ! $gmt )
$i = current_time( 'timestamp' );
else
$i = time();
// we should not let date() interfere with our
// specially computed timestamp
$gmt = true;
}
/*
* Store original value for language with untypical grammars.
* See https://core.trac.wordpress.org/ticket/9396
*/
$req_format = $dateformatstring;
$datefunc = $gmt? 'gmdate' : 'date';
if ( ( !empty( $wp_locale->month ) ) && ( !empty( $wp_locale->weekday ) ) ) {
$datemonth = $wp_locale->get_month( $datefunc( 'm', $i ) );
$datemonth_abbrev = $wp_locale->get_month_abbrev( $datemonth );
$dateweekday = $wp_locale->get_weekday( $datefunc( 'w', $i ) );
$dateweekday_abbrev = $wp_locale->get_weekday_abbrev( $dateweekday );
$datemeridiem = $wp_locale->get_meridiem( $datefunc( 'a', $i ) );
$datemeridiem_capital = $wp_locale->get_meridiem( $datefunc( '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 ( $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 );
}
}
}
}
$j = @$datefunc( $dateformatstring, $i );
/**
* Filter 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 Unix timestamp.
* @param bool $gmt Whether to convert to GMT for time. Default false.
*/
$j = apply_filters( 'date_i18n', $j, $req_format, $i, $gmt );
return $j;
}
更新版本 | 源码位置 | 使用 | 被使用 |
---|---|---|---|
5.3.0 | wp-includes/functions.php:165 | 17 | 5 |
笔记(Notes)
date_i18n()函数的行为基本上与普通的PHP date()函数完全相同,只是它还将诸如月名、工作日和类似的内容转换为站点的当前区域设置。可以使用date()通常采用的参数,将对date()的调用替换为对date的调用。
根据您的博客设置,您将看到以本地格式显示的日期,例如:15。1976年11月。
必须注意日期