From 8d919b09d468b7d30d5389250220cf55d3e11859 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 22 Aug 2019 03:11:55 +0000 Subject: [PATCH] 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 --- wp-includes/class-wp-date-query.php | 18 ++++++++++++------ wp-includes/version.php | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/wp-includes/class-wp-date-query.php b/wp-includes/class-wp-date-query.php index 2f7ef7e28c..67afda2701 100644 --- a/wp-includes/class-wp-date-query.php +++ b/wp-includes/class-wp-date-query.php @@ -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 * 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 * @@ -865,8 +865,6 @@ class WP_Date_Query { * @return string|false A MySQL format date/time or false on failure */ public function build_mysql_datetime( $datetime, $default_to_max = false ) { - $now = current_time( 'timestamp' ); - 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 ( ! is_array( $datetime ) ) { - // @todo Timezone issues here possibly - return gmdate( 'Y-m-d H:i:s', strtotime( $datetime, $now ) ); + $wp_timezone = wp_timezone(); + + // 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 ); if ( ! isset( $datetime['year'] ) ) { - $datetime['year'] = gmdate( 'Y', $now ); + $datetime['year'] = current_time( 'Y' ); } if ( ! isset( $datetime['month'] ) ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index 29763baf05..b514151b5c 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @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.