目录
描述
译文
将GMT格式的日期转换成符合当前博客时间格式的日期。
返回的日期格式为 Y-m-d H:i:s(年-月-日 小时:分:秒)。该函数加上'gmt_offset' 的选项值。
原文
Converts a GMT date into the correct format for the blog.
Requires and returns in the Y-m-d H:i:s format. Simply adds the value of the 'gmt_offset' option.
用法
<?php get_date_from_gmt( $string, $format ) ?>
参数
$string
(string) (必填) The date to be converted.
默认值: None
$format
(string) (false) The format string for the returned date
默认值: Y-m-d H:i:s
返回值
注意
- 使用到: get_option() to retrieve the value of 'gmt_offset'.
历史
添加于 版本: 1.2.0
源文件
get_date_from_gmt() 函数的代码位于 wp-includes/formatting.php
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ /** * Converts a GMT date into the correct format for the blog. * * Requires and returns a date in the Y-m-d H:i:s format. If there is a * timezone_string available, the returned date is in that timezone, otherwise * it simply adds the value of gmt_offset. Return format can be overridden * using the $format parameter * * @since 1.2.0 * * @param string $string The date to be converted. * @param string $format The format string for the returned date (default is Y-m-d H:i:s) * @return string Formatted date relative to the timezone / GMT offset. */ 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; } |
- 原文:http://codex.wordpress.org/Function_Reference/get_date_from_gmt
- 翻译:黄聪@WordPress之魂