Date/Time: Use wp_timezone() in WP_Date_Query::build_mysql_datetime() to address timezone issues.

Improve unit test coverage.

Props Rarst, Biranit, birgire, jave.web, SergeyBiryukov.
Fixes #41782.
Built from https://develop.svn.wordpress.org/trunk@45876


git-svn-id: http://core.svn.wordpress.org/trunk@45687 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2019-08-22 03:11:55 +00:00
parent 1f8563d8e7
commit 8d919b09d4
2 changed files with 13 additions and 7 deletions

View File

@ -853,7 +853,7 @@ class WP_Date_Query {
* *
* You can pass an array of values (year, month, etc.) with missing parameter values being defaulted to * You can pass an array of values (year, month, etc.) with missing parameter values being defaulted to
* either the maximum or minimum values (controlled by the $default_to parameter). Alternatively you can * either the maximum or minimum values (controlled by the $default_to parameter). Alternatively you can
* pass a string that will be run through strtotime(). * pass a string that will be passed to date_create().
* *
* @since 3.7.0 * @since 3.7.0
* *
@ -865,8 +865,6 @@ class WP_Date_Query {
* @return string|false A MySQL format date/time or false on failure * @return string|false A MySQL format date/time or false on failure
*/ */
public function build_mysql_datetime( $datetime, $default_to_max = false ) { public function build_mysql_datetime( $datetime, $default_to_max = false ) {
$now = current_time( 'timestamp' );
if ( ! is_array( $datetime ) ) { if ( ! is_array( $datetime ) ) {
/* /*
@ -907,15 +905,23 @@ class WP_Date_Query {
// If no match is found, we don't support default_to_max. // If no match is found, we don't support default_to_max.
if ( ! is_array( $datetime ) ) { if ( ! is_array( $datetime ) ) {
// @todo Timezone issues here possibly $wp_timezone = wp_timezone();
return gmdate( 'Y-m-d H:i:s', strtotime( $datetime, $now ) );
// Assume local timezone if not provided.
$dt = date_create( $datetime, $wp_timezone );
if ( false === $dt ) {
return gmdate( 'Y-m-d H:i:s', false );
}
return $dt->setTimezone( $wp_timezone )->format( 'Y-m-d H:i:s' );
} }
} }
$datetime = array_map( 'absint', $datetime ); $datetime = array_map( 'absint', $datetime );
if ( ! isset( $datetime['year'] ) ) { if ( ! isset( $datetime['year'] ) ) {
$datetime['year'] = gmdate( 'Y', $now ); $datetime['year'] = current_time( 'Y' );
} }
if ( ! isset( $datetime['month'] ) ) { if ( ! isset( $datetime['month'] ) ) {

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.3-alpha-45875'; $wp_version = '5.3-alpha-45876';
/** /**
* 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.