将GMT日期转换为博客的正确格式。
原型
get_date_from_gmt( string $string, string $format = 'Y-m-d H:i:s' )
描述
需要并以Y-m-d H:i:s格式返回日期。如果有timezone_string可用,则返回的日期在该时区中,否则它只是添加gmt_offset的值。可以使用$ format参数覆盖返回格式
参数
$string
(string)
(Required)
要转换的日期。
$format
(string)
(Optional)
返回日期的格式字符串(默认为Y-m-d H:i:s)
返回值
(string)
格式化日期相对于时区/ GMT偏移量。
源文件
路径:wp-includes/formatting.php
<?php
...
function get_date_from_gmt( $string, $format = 'Y-m-d H:i:s' ) {
$tz = get_option( 'timezone_string' );
if ( $tz ) {
$datetime = date_create( $string, new DateTimeZone( 'UTC' ) );
if ( ! $datetime )
return date( $format, 0 );
$datetime->setTimezone( new DateTimeZone( $tz ) );
$string_localtime = $datetime->format( $format );
} else {
if ( ! preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches) )
return date( $format, 0 );
$string_time = gmmktime( $matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1] );
$string_localtime = gmdate( $format, $string_time + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
}
return $string_localtime;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/get_date_from_gmt/