Make WordPress Core

Changeset 43389


Ignore:
Timestamp:
07/04/2018 11:42:59 PM (6 years ago)
Author:
flixos90
Message:

Date/Time: Fix usage of $gmt parameter in date_i18n() and clarify its behavior.

The docs for date_i18n() and its filter now correctly state that the $gmt parameter is only taken into account if no timestamp is provided. Furthermore, a bug with that parameter is fixed, as it is now ensured that the timezone used with it is UTC.

Props Rarst.
Fixes #38771.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r43387 r43389  
    8989 * @param int|bool $timestamp_with_offset Optional. A sum of Unix timestamp and timezone offset in seconds.
    9090 *                                        Default false.
    91  * @param bool     $gmt                   Optional. Whether to use GMT timezone. Default false.
     91 * @param bool     $gmt                   Optional. Whether to use GMT timezone. Only applies if timestamp is
     92 *                                        not provided. Default false.
    9293 *
    9394 * @return string The date, translated if locale specifies it.
     
    128129    if ( preg_match( "/$timezone_formats_re/", $dateformatstring ) ) {
    129130        $timezone_string = get_option( 'timezone_string' );
     131        if ( false === $timestamp_with_offset && $gmt ) {
     132            $timezone_string = 'UTC';
     133        }
    130134        if ( $timezone_string ) {
    131135            $timezone_object = timezone_open( $timezone_string );
     
    181185     * @param string $req_format Format to display the date.
    182186     * @param int    $i          A sum of Unix timestamp and timezone offset in seconds.
    183      * @param bool   $gmt        Whether to convert to GMT for time. Default false.
     187     * @param bool   $gmt        Whether to use GMT timezone. Only applies if timestamp was
     188     *                           not provided. Default false.
    184189     */
    185190    $j = apply_filters( 'date_i18n', $j, $req_format, $i, $gmt );
  • trunk/tests/phpunit/tests/date/dateI18n.php

    r43387 r43389  
    1515
    1616    public function test_date_should_be_in_gmt() {
    17         $this->assertEquals( strtotime( date( 'Y-m-d H:i:s' ) ), strtotime( date_i18n( 'Y-m-d H:i:s', false, true ) ), 'The dates should be equal', 2 );
     17        $this->assertEquals( strtotime( date( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 'The dates should be equal', 2 );
    1818    }
    1919
     
    3131        update_option( 'timezone_string', 'America/Regina' );
    3232
    33         $this->assertEquals( strtotime( date( 'Y-m-d H:i:s' ) ), strtotime( date_i18n( 'Y-m-d H:i:s', false, true ) ), 'The dates should be equal', 2 );
     33        $this->assertEquals( strtotime( date( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 'The dates should be equal', 2 );
    3434    }
    3535
Note: See TracChangeset for help on using the changeset viewer.