diff --git a/src/wp-includes/class-wp-date-query.php b/src/wp-includes/class-wp-date-query.php index 2f7ef7e28c..67afda2701 100644 --- a/src/wp-includes/class-wp-date-query.php +++ b/src/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/tests/phpunit/tests/date/query.php b/tests/phpunit/tests/date/query.php index 18c2c95218..acf4e5bf7d 100644 --- a/tests/phpunit/tests/date/query.php +++ b/tests/phpunit/tests/date/query.php @@ -509,40 +509,81 @@ class Tests_WP_Date_Query extends WP_UnitTestCase { $this->assertSame( $expected, $found ); } - public function test_build_mysql_datetime_default_to_max_true() { + /** + * @ticket 41782 + * + * @dataProvider mysql_datetime_input_provider + * + * @param array|string $datetime Array or string date input. + * @param string $expected Expected built result. + * @param bool $default_to_max Flag to default missing values to max. + */ + public function test_build_mysql_datetime( $datetime, $expected, $default_to_max = false ) { $q = new WP_Date_Query( array() ); - $found = $q->build_mysql_datetime( - array( - 'year' => 2011, - ), - true - ); - $this->assertSame( '2011-12-31 23:59:59', $found ); + $found = $q->build_mysql_datetime( $datetime, $default_to_max ); + + $message = "Expected {$expected}, got {$found}"; + $this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 ); } - public function test_build_mysql_datetime_default_to_max_false() { - $q = new WP_Date_Query( array() ); - - $found = $q->build_mysql_datetime( - array( - 'year' => 2011, - ), - false + public function mysql_datetime_input_provider() { + return array( + array( '2019-06-04T08:18:24+03:00', '2019-06-04 05:18:24' ), + array( '2019-06-04T05:18:24+00:00', '2019-06-04 05:18:24' ), + array( array(), current_time( 'Y' ) . '-01-01 00:00:00' ), + array( array( 'year' => 2011 ), '2011-12-31 23:59:59', true ), + array( array( 'year' => 2011 ), '2011-01-01 00:00:00' ), + array( '2011', '2011-01-01 00:00:00' ), + array( '2011-02', '2011-02-01 00:00:00' ), + array( '2011-02-03', '2011-02-03 00:00:00' ), + array( '2011-02-03 13:30', '2011-02-03 13:30:00' ), + array( '2011-02-03 13:30:35', '2011-02-03 13:30:35' ), ); - $this->assertSame( '2011-01-01 00:00:00', $found ); } - public function test_build_mysql_datetime_default_to_max_default_to_false() { + /** + * @ticket 41782 + * + * @dataProvider mysql_datetime_input_provider_custom_timezone + * + * @param array|string $datetime Array or string date input. + * @param string $expected Expected built result. + * @param bool $default_to_max Flag to default missing values to max. + */ + public function test_build_mysql_datetime_with_custom_timezone( $datetime, $expected, $default_to_max = false ) { + update_option( 'timezone_string', 'Europe/Kiev' ); + $q = new WP_Date_Query( array() ); - $found = $q->build_mysql_datetime( - array( - 'year' => 2011, - ), - false + $found = $q->build_mysql_datetime( $datetime, $default_to_max ); + + $message = "Expected {$expected}, got {$found}"; + $this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 ); + + } + + public function mysql_datetime_input_provider_custom_timezone() { + return array( + array( '2019-06-04T08:18:24+03:00', '2019-06-04 08:18:24' ), + array( '2019-06-04T05:18:24+00:00', '2019-06-04 08:18:24' ), ); - $this->assertSame( '2011-01-01 00:00:00', $found ); + } + + /** + * @ticket 41782 + */ + public function test_build_mysql_datetime_with_relative_date() { + update_option( 'timezone_string', 'Europe/Kiev' ); + + $q = new WP_Date_Query( array() ); + + $yesterday = new DateTimeImmutable( '-1 day', wp_timezone() ); + $expected = $yesterday->format( 'Y-m-d H:i:s' ); + $found = $q->build_mysql_datetime( '-1 day' ); + + $message = "Expected {$expected}, got {$found}"; + $this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 ); } public function test_build_time_query_insufficient_time_values() {