From 21190f2eac02fdb89deee1ada00a15bdd6dbfa90 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 4 Jul 2018 23:42:59 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@43389 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 9 +++++++-- tests/phpunit/tests/date/dateI18n.php | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index b4df760ec6..94005d65a0 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -88,7 +88,8 @@ function current_time( $type, $gmt = 0 ) { * @param string $dateformatstring Format to display the date. * @param int|bool $timestamp_with_offset Optional. A sum of Unix timestamp and timezone offset in seconds. * Default false. - * @param bool $gmt Optional. Whether to use GMT timezone. Default false. + * @param bool $gmt Optional. Whether to use GMT timezone. Only applies if timestamp is + * not provided. Default false. * * @return string The date, translated if locale specifies it. */ @@ -127,6 +128,9 @@ function date_i18n( $dateformatstring, $timestamp_with_offset = false, $gmt = fa $timezone_formats_re = implode( '|', $timezone_formats ); if ( preg_match( "/$timezone_formats_re/", $dateformatstring ) ) { $timezone_string = get_option( 'timezone_string' ); + if ( false === $timestamp_with_offset && $gmt ) { + $timezone_string = 'UTC'; + } if ( $timezone_string ) { $timezone_object = timezone_open( $timezone_string ); $date_object = date_create( null, $timezone_object ); @@ -180,7 +184,8 @@ function date_i18n( $dateformatstring, $timestamp_with_offset = false, $gmt = fa * @param string $j Formatted date string. * @param string $req_format Format to display the date. * @param int $i A sum of Unix timestamp and timezone offset in seconds. - * @param bool $gmt Whether to convert to GMT for time. Default false. + * @param bool $gmt Whether to use GMT timezone. Only applies if timestamp was + * not provided. Default false. */ $j = apply_filters( 'date_i18n', $j, $req_format, $i, $gmt ); return $j; diff --git a/tests/phpunit/tests/date/dateI18n.php b/tests/phpunit/tests/date/dateI18n.php index d5685190c7..283d82adbd 100644 --- a/tests/phpunit/tests/date/dateI18n.php +++ b/tests/phpunit/tests/date/dateI18n.php @@ -14,7 +14,7 @@ class Tests_Date_I18n extends WP_UnitTestCase { } public function test_date_should_be_in_gmt() { - $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 ); + $this->assertEquals( strtotime( date( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 'The dates should be equal', 2 ); } public function test_custom_timestamp_ignores_gmt_setting() { @@ -30,7 +30,7 @@ class Tests_Date_I18n extends WP_UnitTestCase { public function test_date_should_be_in_gmt_with_custom_timezone_setting() { update_option( 'timezone_string', 'America/Regina' ); - $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 ); + $this->assertEquals( strtotime( date( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 'The dates should be equal', 2 ); } public function test_date_should_be_in_gmt_with_custom_timezone_setting_and_timestamp() {