Make WordPress Core


Ignore:
Timestamp:
08/22/2019 03:10:05 AM (7 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-date-query.php

    r45850 r45876  
    854854         * You can pass an array of values (year, month, etc.) with missing parameter values being defaulted to
    855855         * 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().
    857857         *
    858858         * @since 3.7.0
    … …  
    866866         */
    867867        public function build_mysql_datetime( $datetime, $default_to_max = false ) {
    868                 $now = current_time( 'timestamp' );
    869 
    870868                if ( ! is_array( $datetime ) ) {
    871869
    … …  
    908906                        // If no match is found, we don't support default_to_max.
    909907                        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' );
    912918                        }
    913919                }
    … …  
    916922
    917923                if ( ! isset( $datetime['year'] ) ) {
    918                         $datetime['year'] = gmdate( 'Y', $now );
     924                        $datetime['year'] = current_time( 'Y' );
    919925                }
    920926
Note: See TracChangeset for help on using the changeset viewer.