WordPress.org

Make WordPress Core

Changeset 45908


Ignore:
Timestamp:
08/29/2019 05:06:15 AM (3 weeks ago)
Author:
SergeyBiryukov
Message:

Date/Time: Revamp mysql2date() to use wp_date() and handle invalid input in a consistent manner.

Add unit tests, improve documentation.

Props Rarst, pbearne.
Fixes #28992.

Location:
trunk
Files:
1 added
1 edited

Legend:

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

    r45901 r45908  
    99
    1010/**
    11  * Convert given date string into a different format.
    12  *
    13  * $format should be either a PHP date format string, e.g. 'U' for a Unix
    14  * timestamp, or 'G' for a Unix timestamp assuming that $date is GMT.
    15  *
    16  * If $translate is true then the given date and format string will
    17  * be passed to date_i18n() for translation.
     11 * Convert given MySQL date string into a different format.
     12 *
     13 * `$format` should be a PHP date format string.
     14 * 'U' and 'G' formats will return a sum of timestamp with timezone offset.
     15 * `$date` is expected to be local time in MySQL format (`Y-m-d H:i:s`).
     16 *
     17 * Historically UTC time could be passed to the function to produce Unix timestamp.
     18 *
     19 * If `$translate` is true then the given date and format string will
     20 * be passed to `wp_date()` for translation.
    1821 *
    1922 * @since 0.71
     
    2225 * @param string $date      Date string to convert.
    2326 * @param bool   $translate Whether the return date should be translated. Default true.
    24  * @return string|int|bool Formatted date string or Unix timestamp. False if $date is empty.
     27 * @return string|int|false Formatted date string or sum of Unix timestamp and timezone offset.
     28 *                          False on failure.
    2529 */
    2630function mysql2date( $format, $date, $translate = true ) {
     
    2933    }
    3034
    31     if ( 'G' == $format ) {
    32         return strtotime( $date . ' +0000' );
    33     }
    34 
    35     $i = strtotime( $date );
    36 
    37     if ( 'U' == $format ) {
    38         return $i;
     35    $datetime = date_create( $date, wp_timezone() );
     36
     37    if ( false === $datetime ) {
     38        return false;
     39    }
     40
     41    // Returns a sum of timestamp with timezone offset. Ideally should never be used.
     42    if ( 'G' === $format || 'U' === $format ) {
     43        return $datetime->getTimestamp() + $datetime->getOffset();
    3944    }
    4045
    4146    if ( $translate ) {
    42         return date_i18n( $format, $i );
    43     } else {
    44         return gmdate( $format, $i );
    45     }
     47        return wp_date( $format, $datetime->getTimestamp() );
     48    }
     49
     50    return $datetime->format( $format );
    4651}
    4752
     
    5964 * @since 1.0.0
    6065 *
    61  * @param string   $type Type of time to retrieve. Accepts 'mysql', 'timestamp', or PHP date
    62  *                       format string (e.g. 'Y-m-d').
     66 * @param string   $type Type of time to retrieve. Accepts 'mysql', 'timestamp',
     67 *                       or PHP date format string (e.g. 'Y-m-d').
    6368 * @param int|bool $gmt  Optional. Whether to use GMT timezone. Default false.
    6469 * @return int|string Integer if $type is 'timestamp', string otherwise.
     
    152157 *
    153158 * @param string   $format                Format to display the date.
    154  * @param int|bool $timestamp_with_offset Optional. A sum of Unix timestamp and timezone offset in seconds.
    155  *                                        Default false.
    156  * @param bool     $gmt                   Optional. Whether to use GMT timezone. Only applies if timestamp is
    157  *                                        not provided. Default false.
     159 * @param int|bool $timestamp_with_offset Optional. A sum of Unix timestamp and timezone offset
     160 *                                        in seconds. Default false.
     161 * @param bool     $gmt                   Optional. Whether to use GMT timezone. Only applies
     162 *                                        if timestamp is not provided. Default false.
    158163 * @return string The date, translated if locale specifies it.
    159164 */
     
    205210 * This is a newer function, intended to replace `date_i18n()` without legacy quirks in it.
    206211 *
    207  * Note that, unlike `date_i18n()`, this function accepts a true Unix timestamp, not summed with timezone offset.
     212 * Note that, unlike `date_i18n()`, this function accepts a true Unix timestamp, not summed
     213 * with timezone offset.
    208214 *
    209215 * @since 5.3.0
     
    211217 * @param string       $format    PHP date format.
    212218 * @param int          $timestamp Optional. Unix timestamp. Defaults to current time.
    213  * @param DateTimeZone $timezone  Optional. Timezone to output result in. Defaults to timezone from site settings.
    214  *
     219 * @param DateTimeZone $timezone  Optional. Timezone to output result in. Defaults to timezone
     220 *                                from site settings.
    215221 * @return string The date, translated if locale specifies it.
    216222 */
Note: See TracChangeset for help on using the changeset viewer.