函数原型
get_post_datetime( int|WP_Post $post = null, string $field = ‘date’, string $source = ‘local’ ): DateTimeImmutable|false
函数描述
Retrieves post published or modified time as a DateTimeImmutable object instance.
是否弃用
未弃用
函数参数
-
$post
int|WP_Post
Optional - Post ID or post object. Default is global
$postobject.Default:
null -
$field
string
Optional - Published or modified time to use from database. Accepts
'date'or'modified'.
Default'date'.Default:
'date' -
$source
string
Optional - Local or UTC time to use from database. Accepts
'local'or'gmt'.
Default'local'.Default:
'local'
函数返回值
DateTimeImmutable|false Time object on success, false on failure.
函数位置
File: wp-includes/general-template.php.
函数源码
function get_post_datetime( $post = null, $field = 'date', $source = 'local' ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$wp_timezone = wp_timezone();
if ( 'gmt' === $source ) {
$time = ( 'modified' === $field ) ? $post->post_modified_gmt : $post->post_date_gmt;
$timezone = new DateTimeZone( 'UTC' );
} else {
$time = ( 'modified' === $field ) ? $post->post_modified : $post->post_date;
$timezone = $wp_timezone;
}
if ( empty( $time ) || '0000-00-00 00:00:00' === $time ) {
return false;
}
$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', $time, $timezone );
if ( false === $datetime ) {
return false;
}
return $datetime->setTimezone( $wp_timezone );
}
源码链接
变更日志
| Version | Description |
|---|---|
| 5.3.0 | Introduced. |

