Date/Time: Make sure `get_post_time()` keeps UTC time on timezone change.
Add `$source` parameter to `get_post_datetime()` to instantiate from local or UTC time in database. Props Rarst, david.binda. Reviewed by azaozz, SergeyBiryukov. Fixes #48384. Built from https://develop.svn.wordpress.org/trunk@46580 git-svn-id: http://core.svn.wordpress.org/trunk@46377 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
7e11e9d732
commit
75c183c248
|
@ -2565,7 +2565,8 @@ function get_post_time( $d = 'U', $gmt = false, $post = null, $translate = false
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$datetime = get_post_datetime( $post );
|
$source = ( $gmt ) ? 'gmt' : 'local';
|
||||||
|
$datetime = get_post_datetime( $post, 'date', $source );
|
||||||
|
|
||||||
if ( false === $datetime ) {
|
if ( false === $datetime ) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -2606,26 +2607,48 @@ function get_post_time( $d = 'U', $gmt = false, $post = null, $translate = false
|
||||||
*
|
*
|
||||||
* The object will be set to the timezone from WordPress settings.
|
* The object will be set to the timezone from WordPress settings.
|
||||||
*
|
*
|
||||||
|
* For legacy reasons, this function allows to choose to instantiate from local or UTC time in database.
|
||||||
|
* Normally this should make no difference to the result. However, the values might get out of sync in database,
|
||||||
|
* typically because of timezone setting changes. The parameter ensures the ability to reproduce backwards
|
||||||
|
* compatible behaviors in such cases.
|
||||||
|
*
|
||||||
* @since 5.3.0
|
* @since 5.3.0
|
||||||
*
|
*
|
||||||
* @param int|WP_Post $post Optional. WP_Post object or ID. Default is global `$post` object.
|
* @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'.
|
* @param string $field Optional. Published or modified time to use from database. Accepts 'date' or 'modified'.
|
||||||
|
* Default 'date'.
|
||||||
|
* @param string $source Optional. Local or UTC time to use from database. Accepts 'local' or 'gmt'.
|
||||||
|
* Default 'local'.
|
||||||
* @return DateTimeImmutable|false Time object on success, false on failure.
|
* @return DateTimeImmutable|false Time object on success, false on failure.
|
||||||
*/
|
*/
|
||||||
function get_post_datetime( $post = null, $field = 'date' ) {
|
function get_post_datetime( $post = null, $field = 'date', $source = 'local' ) {
|
||||||
$post = get_post( $post );
|
$post = get_post( $post );
|
||||||
|
|
||||||
if ( ! $post ) {
|
if ( ! $post ) {
|
||||||
return false;
|
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;
|
$time = ( 'modified' === $field ) ? $post->post_modified : $post->post_date;
|
||||||
|
$timezone = $wp_timezone;
|
||||||
|
}
|
||||||
|
|
||||||
if ( empty( $time ) || '0000-00-00 00:00:00' === $time ) {
|
if ( empty( $time ) || '0000-00-00 00:00:00' === $time ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return date_create_immutable_from_format( 'Y-m-d H:i:s', $time, wp_timezone() );
|
$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', $time, $timezone );
|
||||||
|
|
||||||
|
if ( false === $datetime ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $datetime->setTimezone( $wp_timezone );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2637,7 +2660,8 @@ function get_post_datetime( $post = null, $field = 'date' ) {
|
||||||
* @since 5.3.0
|
* @since 5.3.0
|
||||||
*
|
*
|
||||||
* @param int|WP_Post $post Optional. WP_Post object or ID. Default is global `$post` object.
|
* @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'.
|
* @param string $field Optional. Published or modified time to use from database. Accepts 'date' or 'modified'.
|
||||||
|
* Default 'date'.
|
||||||
* @return int|false Unix timestamp on success, false on failure.
|
* @return int|false Unix timestamp on success, false on failure.
|
||||||
*/
|
*/
|
||||||
function get_post_timestamp( $post = null, $field = 'date' ) {
|
function get_post_timestamp( $post = null, $field = 'date' ) {
|
||||||
|
@ -2729,7 +2753,8 @@ function get_post_modified_time( $d = 'U', $gmt = false, $post = null, $translat
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$datetime = get_post_datetime( $post, 'modified' );
|
$source = ( $gmt ) ? 'gmt' : 'local';
|
||||||
|
$datetime = get_post_datetime( $post, 'modified', $source );
|
||||||
|
|
||||||
if ( false === $datetime ) {
|
if ( false === $datetime ) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '5.3-RC2-46579';
|
$wp_version = '5.3-RC2-46580';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue