Ticket #41782: 41782.diff
File 41782.diff, 5.4 KB (added by , 6 years ago) |
---|
-
src/wp-includes/class-wp-date-query.php
853 853 * 854 854 * You can pass an array of values (year, month, etc.) with missing parameter values being defaulted to 855 855 * either the maximum or minimum values (controlled by the $default_to parameter). Alternatively you can 856 * pass a string that will be run through strtotime().856 * pass a string that will be passed to date_create(). 857 857 * 858 858 * @since 3.7.0 859 859 * … … 865 865 * @return string|false A MySQL format date/time or false on failure 866 866 */ 867 867 public function build_mysql_datetime( $datetime, $default_to_max = false ) { 868 $now = current_time( 'timestamp' );869 870 868 if ( ! is_array( $datetime ) ) { 871 869 872 870 /* … … 907 905 908 906 // If no match is found, we don't support default_to_max. 909 907 if ( ! is_array( $datetime ) ) { 910 // @todo Timezone issues here possibly 911 return gmdate( 'Y-m-d H:i:s', strtotime( $datetime, $now ) ); 908 $wp_timezone = wp_timezone(); 909 910 // Assume local timezone if not provided. 911 $dt = date_create( $datetime, $wp_timezone ); 912 913 if ( false === $dt ) { 914 return gmdate( 'Y-m-d H:i:s', false ); 915 } 916 917 return $dt->setTimezone( $wp_timezone )->format( 'Y-m-d H:i:s' ); 912 918 } 913 919 } 914 920 … … 915 921 $datetime = array_map( 'absint', $datetime ); 916 922 917 923 if ( ! isset( $datetime['year'] ) ) { 918 $datetime['year'] = gmdate( 'Y', $now);924 $datetime['year'] = current_time( 'Y' ); 919 925 } 920 926 921 927 if ( ! isset( $datetime['month'] ) ) { -
tests/phpunit/tests/date/query.php
509 509 $this->assertSame( $expected, $found ); 510 510 } 511 511 512 public function test_build_mysql_datetime_default_to_max_true() { 512 /** 513 * @ticket 41782 514 * 515 * @dataProvider mysql_datetime_input_provider 516 * 517 * @param array|string $datetime Array or string date input. 518 * @param string $expected Expected built result. 519 * @param bool $default_to_max Flag to default missing values to max. 520 */ 521 public function test_build_mysql_datetime( $datetime, $expected, $default_to_max = false ) { 513 522 $q = new WP_Date_Query( array() ); 514 523 515 $found = $q->build_mysql_datetime( 516 array( 517 'year' => 2011, 518 ), 519 true 524 $found = $q->build_mysql_datetime( $datetime, $default_to_max ); 525 526 $message = "Expected {$expected}, got {$found}"; 527 $this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 ); 528 } 529 530 public function mysql_datetime_input_provider() { 531 return array( 532 array( '2019-06-04T08:18:24+03:00', '2019-06-04 05:18:24' ), 533 array( '2019-06-04T05:18:24+00:00', '2019-06-04 05:18:24' ), 534 array( array(), current_time( 'Y' ) . '-01-01 00:00:00' ), 535 array( array( 'year' => 2011 ), '2011-12-31 23:59:59', true ), 536 array( array( 'year' => 2011 ), '2011-01-01 00:00:00' ), 537 array( '2011', '2011-01-01 00:00:00' ), 538 array( '2011-02', '2011-02-01 00:00:00' ), 539 array( '2011-02-03', '2011-02-03 00:00:00' ), 540 array( '2011-02-03 13:30', '2011-02-03 13:30:00' ), 541 array( '2011-02-03 13:30:35', '2011-02-03 13:30:35' ), 520 542 ); 521 $this->assertSame( '2011-12-31 23:59:59', $found );522 543 } 523 544 524 public function test_build_mysql_datetime_default_to_max_false() { 545 /** 546 * @ticket 41782 547 * 548 * @dataProvider mysql_datetime_input_provider_custom_timezone 549 * 550 * @param array|string $datetime Array or string date input. 551 * @param string $expected Expected built result. 552 * @param bool $default_to_max Flag to default missing values to max. 553 */ 554 public function test_build_mysql_datetime_with_custom_timezone( $datetime, $expected, $default_to_max = false ) { 555 update_option( 'timezone_string', 'Europe/Kiev' ); 556 525 557 $q = new WP_Date_Query( array() ); 526 558 527 $found = $q->build_mysql_datetime( 528 array( 529 'year' => 2011, 530 ), 531 false 559 $found = $q->build_mysql_datetime( $datetime, $default_to_max ); 560 561 $message = "Expected {$expected}, got {$found}"; 562 $this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 ); 563 564 } 565 566 public function mysql_datetime_input_provider_custom_timezone() { 567 return array( 568 array( '2019-06-04T08:18:24+03:00', '2019-06-04 08:18:24' ), 569 array( '2019-06-04T05:18:24+00:00', '2019-06-04 08:18:24' ), 532 570 ); 533 $this->assertSame( '2011-01-01 00:00:00', $found );534 571 } 535 572 536 public function test_build_mysql_datetime_default_to_max_default_to_false() { 573 /** 574 * @ticket 41782 575 */ 576 public function test_build_mysql_datetime_with_relative_date() { 577 update_option( 'timezone_string', 'Europe/Kiev' ); 578 537 579 $q = new WP_Date_Query( array() ); 538 580 539 $found = $q->build_mysql_datetime( 540 array( 541 'year' => 2011, 542 ), 543 false 544 ); 545 $this->assertSame( '2011-01-01 00:00:00', $found ); 581 $yesterday = new DateTimeImmutable( '-1 day', wp_timezone() ); 582 $expected = $yesterday->format( 'Y-m-d H:i:s' ); 583 $found = $q->build_mysql_datetime( '-1 day' ); 584 585 $message = "Expected {$expected}, got {$found}"; 586 $this->assertEquals( strtotime( $expected ), strtotime( $found ), $message, 10 ); 546 587 } 547 588 548 589 public function test_build_time_query_insufficient_time_values() {