Changeset 45908
- Timestamp:
- 08/29/2019 05:06:15 AM (5 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/functions.php
r45901 r45908 9 9 10 10 /** 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. 18 21 * 19 22 * @since 0.71 … … 22 25 * @param string $date Date string to convert. 23 26 * @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. 25 29 */ 26 30 function mysql2date( $format, $date, $translate = true ) { … … 29 33 } 30 34 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(); 39 44 } 40 45 41 46 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 ); 46 51 } 47 52 … … 59 64 * @since 1.0.0 60 65 * 61 * @param string $type Type of time to retrieve. Accepts 'mysql', 'timestamp', or PHP date62 * 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'). 63 68 * @param int|bool $gmt Optional. Whether to use GMT timezone. Default false. 64 69 * @return int|string Integer if $type is 'timestamp', string otherwise. … … 152 157 * 153 158 * @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 is157 * 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. 158 163 * @return string The date, translated if locale specifies it. 159 164 */ … … 205 210 * This is a newer function, intended to replace `date_i18n()` without legacy quirks in it. 206 211 * 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. 208 214 * 209 215 * @since 5.3.0 … … 211 217 * @param string $format PHP date format. 212 218 * @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. 215 221 * @return string The date, translated if locale specifies it. 216 222 */
Note: See TracChangeset
for help on using the changeset viewer.