From 349e9f56c11fcce3818658172d65244e0d3ab14a Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 19 Aug 2019 21:07:50 +0000 Subject: [PATCH] Date/Time: Use PHP `DateTime` class API in `current_time()`. Only use the legacy WP timestamp approach (a sum of timestamp and timezone offset) for `timestamp` and `U` formats without the `$gmt` flag. Otherwise, make sure the function returns correct local time for any format. Props Rarst, jdgrimes. Fixes #40653. Built from https://develop.svn.wordpress.org/trunk@45856 git-svn-id: http://core.svn.wordpress.org/trunk@45667 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/functions.php | 24 +++++++++++++++--------- wp-includes/version.php | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 8e17499be0..857d16fd60 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -46,10 +46,11 @@ function mysql2date( $format, $date, $translate = true ) { } /** - * Retrieve the current time based on specified type. + * Retrieves the current time based on specified type. * * The 'mysql' type will return the time in the format for MySQL DATETIME field. - * The 'timestamp' type will return the current timestamp. + * The 'timestamp' type will return the current timestamp or a sum of timestamp + * and timezone offset, depending on `$gmt`. * Other strings will be interpreted as PHP date formats (e.g. 'Y-m-d'). * * If $gmt is set to either '1' or 'true', then both types will use GMT time. @@ -63,14 +64,19 @@ function mysql2date( $format, $date, $translate = true ) { * @return int|string Integer if $type is 'timestamp', string otherwise. */ function current_time( $type, $gmt = 0 ) { - switch ( $type ) { - case 'mysql': - return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ) ); - case 'timestamp': - return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); - default: - return ( $gmt ) ? gmdate( $type ) : gmdate( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ); + // Don't use non-GMT timestamp, unless you know the difference and really need to. + if ( 'timestamp' === $type || 'U' === $type ) { + return $gmt ? time() : time() + (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); } + + if ( 'mysql' === $type ) { + $type = 'Y-m-d H:i:s'; + } + + $timezone = $gmt ? new DateTimeZone( 'UTC' ) : wp_timezone(); + $datetime = new DateTime( 'now', $timezone ); + + return $datetime->format( $type ); } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 74a2164eb6..d35b21c18f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.3-alpha-45855'; +$wp_version = '5.3-alpha-45856'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.