Date/Time: Introduce get_post_datetime() to retrieve post published or modified time as a DateTimeImmutable object instance.

Introduce `get_post_timestamp()` to retrieve post published or modified time as a Unix timestamp.

Use `get_post_datetime()` in `get_post_time()` and `get_post_modified_time()` to return correct GMT time if default timezone is changed from UTC.

Props Rarst, johnregan3.
Fixes #25002.
Built from https://develop.svn.wordpress.org/trunk@46154


git-svn-id: http://core.svn.wordpress.org/trunk@45966 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2019-09-17 11:14:54 +00:00
parent 757d077669
commit b2a384461e
2 changed files with 93 additions and 14 deletions

View File

@ -2515,7 +2515,7 @@ function the_time( $d = '' ) {
* @param string $d Optional. Format to use for retrieving the time the post
* was written. Either 'G', 'U', or php date format defaults
* to the value specified in the time_format option. Default empty.
* @param int|WP_Post $post WP_Post object or ID. Default is global $post object.
* @param int|WP_Post $post WP_Post object or ID. Default is global `$post` object.
* @return string|int|false Formatted date string or Unix timestamp if `$d` is 'U' or 'G'. False on failure.
*/
function get_the_time( $d = '', $post = null ) {
@ -2553,7 +2553,7 @@ function get_the_time( $d = '', $post = null ) {
* @param string $d Optional. Format to use for retrieving the time the post
* was written. Either 'G', 'U', or php date format. Default 'U'.
* @param bool $gmt Optional. Whether to retrieve the GMT time. Default false.
* @param int|WP_Post $post WP_Post object or ID. Default is global $post object.
* @param int|WP_Post $post WP_Post object or ID. Default is global `$post` object.
* @param bool $translate Whether to translate the time string. Default false.
* @return string|int|false Formatted date string or Unix timestamp if `$d` is 'U' or 'G'. False on failure.
*/
@ -2564,13 +2564,28 @@ function get_post_time( $d = 'U', $gmt = false, $post = null, $translate = false
return false;
}
if ( $gmt ) {
$time = $post->post_date_gmt;
} else {
$time = $post->post_date;
$datetime = get_post_datetime( $post );
if ( false === $datetime ) {
return false;
}
$time = mysql2date( $d, $time, $translate );
if ( 'U' === $d || 'G' === $d ) {
$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( $d, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null );
} else {
if ( $gmt ) {
$datetime = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
}
$time = $datetime->format( $d );
}
/**
* Filters the localized time a post was written.
@ -2585,6 +2600,55 @@ function get_post_time( $d = 'U', $gmt = false, $post = null, $translate = false
return apply_filters( 'get_post_time', $time, $d, $gmt );
}
/**
* Retrieve post published or modified time as a `DateTimeImmutable` object instance.
*
* The object will be set to the timezone from WordPress settings.
*
* @since 5.3.0
*
* @param int|WP_Post $post Optional. WP_Post object or ID. Default is global `$post` object.
* @param string $field Optional. Post field to use. Accepts 'date' or 'modified'.
* @return DateTimeImmutable|false Time object on success, false on failure.
*/
function get_post_datetime( $post = null, $field = 'date' ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$time = ( 'modified' === $field ) ? $post->post_modified : $post->post_date;
if ( empty( $time ) || '0000-00-00 00:00:00' === $time ) {
return false;
}
return date_create_immutable_from_format( 'Y-m-d H:i:s', $time, wp_timezone() );
}
/**
* Retrieve post published or modified time as a Unix timestamp.
*
* Note that this function returns a true Unix timestamp, not summed with timezone offset
* like older WP functions.
*
* @since 5.3.0
*
* @param int|WP_Post $post Optional. WP_Post object or ID. Default is global `$post` object.
* @param string $field Optional. Post field to use. Accepts 'date' or 'modified'.
* @return int|false Unix timestamp on success, false on failure.
*/
function get_post_timestamp( $post = null, $field = 'date' ) {
$datetime = get_post_datetime( $post, $field );
if ( false === $datetime ) {
return false;
}
return $datetime->getTimestamp();
}
/**
* Display the time at which the post was last modified.
*
@ -2653,7 +2717,7 @@ function get_the_modified_time( $d = '', $post = null ) {
* @param string $d Optional. Format to use for retrieving the time the post
* was modified. Either 'G', 'U', or php date format. Default 'U'.
* @param bool $gmt Optional. Whether to retrieve the GMT time. Default false.
* @param int|WP_Post $post WP_Post object or ID. Default is global $post object.
* @param int|WP_Post $post WP_Post object or ID. Default is global `$post` object.
* @param bool $translate Whether to translate the time string. Default false.
* @return string|int|false Formatted date string or Unix timestamp if `$d` is 'U' or 'G'. False on failure.
*/
@ -2664,13 +2728,28 @@ function get_post_modified_time( $d = 'U', $gmt = false, $post = null, $translat
return false;
}
if ( $gmt ) {
$time = $post->post_modified_gmt;
} else {
$time = $post->post_modified;
$datetime = get_post_datetime( $post, 'modified' );
if ( false === $datetime ) {
return false;
}
$time = mysql2date( $d, $time, $translate );
if ( 'U' === $d || 'G' === $d ) {
$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( $d, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null );
} else {
if ( $gmt ) {
$datetime = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
}
$time = $datetime->format( $d );
}
/**
* Filters the localized time a post was last modified.

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.3-alpha-46153';
$wp_version = '5.3-alpha-46154';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.