get_post_time()
云策文档标注
概述
get_post_time() 函数用于获取文章的本地化时间,支持多种格式和时区选项。它基于 get_post_datetime() 实现,并可通过过滤器进行自定义。
关键要点
- 函数返回文章的本地化时间,格式可以是字符串、Unix 时间戳或 false(失败时)。
- 参数包括 $format(格式,默认 'U')、$gmt(是否使用 GMT 时间,默认 false)、$post(文章 ID 或对象,默认全局 $post)和 $translate(是否翻译时间字符串,默认 false)。
- 内部使用 get_post_datetime() 获取 DateTimeImmutable 对象,并根据参数处理时间输出。
- 提供 'get_post_time' 过滤器钩子,允许开发者修改返回的时间值。
代码示例
function get_post_time( $format = 'U', $gmt = false, $post = null, $translate = false ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$source = ( $gmt ) ? 'gmt' : 'local';
$datetime = get_post_datetime( $post, 'date', $source );
if ( false === $datetime ) {
return false;
}
if ( 'U' === $format || 'G' === $format ) {
$time = $datetime->getTimestamp();
// Returns a sum of timestamp with timezone offset. Ideally should never be used.
if ( ! $gmt ) {
$time += $datetime->getOffset();
}
} elseif ( $translate ) {
$time = wp_date( $format, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null );
} else {
if ( $gmt ) {
$datetime = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
}
$time = $datetime->format( $format );
}
/**
* Filters the localized time of the post.
*
* @since 2.6.0
*
* @param string|int $time Formatted date string or Unix timestamp if `$format` is 'U' or 'G'.
* @param string $format Format to use for retrieving the date of the post.
* Accepts 'G', 'U', or PHP date format.
* @param bool $gmt Whether to retrieve the GMT time.
*/
return apply_filters( 'get_post_time', $time, $format, $gmt );
}注意事项
- 当 $format 为 'U' 或 'G' 时,返回 Unix 时间戳;其他 PHP 日期格式返回字符串。
- 如果 $gmt 为 false,在非 GMT 模式下计算时间戳时会加上时区偏移,这可能不是最佳实践。
- 函数自 WordPress 2.0.0 版本引入,相关函数包括 wp_date()、get_post_datetime() 等。
原文内容
Retrieves the localized time of the post.
Parameters
$formatstringoptional-
Format to use for retrieving the time the post was written. Accepts
'G','U', or PHP date format. Default'U'. $gmtbooloptional-
Whether to retrieve the GMT time.
Default:
false $postint|WP_Postoptional-
Post ID or post object. Default is global
$postobject.Default:
null $translatebooloptional-
Whether to translate the time string.
Default:
false
Source
function get_post_time( $format = 'U', $gmt = false, $post = null, $translate = false ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$source = ( $gmt ) ? 'gmt' : 'local';
$datetime = get_post_datetime( $post, 'date', $source );
if ( false === $datetime ) {
return false;
}
if ( 'U' === $format || 'G' === $format ) {
$time = $datetime->getTimestamp();
// Returns a sum of timestamp with timezone offset. Ideally should never be used.
if ( ! $gmt ) {
$time += $datetime->getOffset();
}
} elseif ( $translate ) {
$time = wp_date( $format, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null );
} else {
if ( $gmt ) {
$datetime = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
}
$time = $datetime->format( $format );
}
/**
* Filters the localized time of the post.
*
* @since 2.6.0
*
* @param string|int $time Formatted date string or Unix timestamp if `$format` is 'U' or 'G'.
* @param string $format Format to use for retrieving the date of the post.
* Accepts 'G', 'U', or PHP date format.
* @param bool $gmt Whether to retrieve the GMT time.
*/
return apply_filters( 'get_post_time', $time, $format, $gmt );
}
Hooks
- apply_filters( ‘get_post_time’, string|int $time, string $format, bool $gmt )
-
Filters the localized time of the post.
Changelog
| Version | Description |
|---|---|
| 2.0.0 | Introduced. |
Skip to note 2 content
Gwyneth Llewelyn
For a complete list of date/time formatting options, the ‘PHP date format’ is available from the PHP.net reference website.