From f1862cd98c296e7ce6921a129b4da796980e85f2 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 30 Aug 2018 12:12:55 +0000 Subject: [PATCH] Date/Time: Make sure `current_time()` always calculates the offset from GMT/UTC. Previously, UTC context was implied, but could be unreliable due to being affected by plugins. Props Rarst, rahulsprajapati, marco.marsala. Fixes #37440. git-svn-id: https://develop.svn.wordpress.org/trunk@43594 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 2 +- tests/phpunit/tests/date/currentTime.php | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/date/currentTime.php diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 16032f465a..61395c987f 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -69,7 +69,7 @@ function current_time( $type, $gmt = 0 ) { case 'timestamp': return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); default: - return ( $gmt ) ? date( $type ) : date( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ); + return ( $gmt ) ? gmdate( $type ) : gmdate( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ); } } diff --git a/tests/phpunit/tests/date/currentTime.php b/tests/phpunit/tests/date/currentTime.php new file mode 100644 index 0000000000..209eb128c6 --- /dev/null +++ b/tests/phpunit/tests/date/currentTime.php @@ -0,0 +1,24 @@ +assertEquals( gmdate( $format ), current_time( $format, true ) ); + $this->assertEquals( $datetime->format( $format ), current_time( $format ) ); + + date_default_timezone_set( 'UTC' ); + $this->assertEquals( gmdate( $format ), current_time( $format, true ) ); + $this->assertEquals( $datetime->format( $format ), current_time( $format ) ); + } +}