WordPress函数 wp_date 详解,这是从wordpress5.3版本开始新增的函数,旨在解决不同系统中日期显示问题,今天在新增最新文章发布日期标红功能时,发现PHP自带的strtotime函数无法正确转换时间戳,搜索后得知wordpress已经将此函数转换时区固定为UTC。
复制
wp_date( string $format, int $timestamp = null, DateTimeZone $timezone = null ): string|false
描述
以本地化格式检索日期。这是一个较新的功能,旨在取代date_i18n()它而没有遗留的怪癖。
请注意,与 不同date_i18n(),此函数接受真正的 Unix 时间戳,而不是与时区偏移相加。
参数
- $format 字符串 必填
PHP 日期格式。$timestamp可选 的 - Unix 时间戳。默认为当前时间。
默认:null - $timezone DateTimeZone 可选
输出结果的时区。默认为站点设置中的时区。
默认:null
返回值
string|false日期,如果语言环境指定则翻译。False 无效的时间戳输入。
函数原型
此函数位于:wp-includes/functions.php
复制
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;
}
Hooks
复制
apply_filters ('wp_date', string $date , string $format , int $timestamp , DateTimeZone $timezone )根据语言环境过滤格式化的日期。
简单使用
复制
echo wp_date('Y-m-d H:i:s', 1669043745);日期转时间戳
PHP的 strtotime 函数转换结果不正确,因为默认时区已被修改为UTC。请改为如下方法。
复制
date_create($string, wp_timezone())->getTimestamp();
wp_timezone() 获取当前站点的时区,但是返回的是 DateTimeZone 对象。
当然你也可以使用 timezone_open("Europe/Oslo") 函数指定时区,生成对象。





评论 (2)