Converts given MySQL date string into a different format.
Description
<ul> <li>$format should be a PHP date format string.</li> <li>'U' and 'G' formats will return an integer sum of timestamp with timezone offset.</li> <li>$date is expected to be local time in MySQL format (Y-m-d H:i:s).</li> </ul> Historically UTC time could be passed to the function to produce Unix timestamp. If $translate is true then the given date and format string will be passed to wp_date() for translation.
Parameters
$formatstring
Format of the date to return.
$datestring
Date string to convert.
$translatebooloptional
Whether the return date should be translated. Default true.Default: true
Return
string|int|false
Integer if $format is 'U' or 'G', string otherwise. False on failure.
Uses · 2
wp_timezone()Retrieves the timezone of the site as a `DateTimeZone` object.
35functionmysql2date($format,$date,$translate=true){36if(empty($date)){37returnfalse;38}3940$timezone=wp_timezone();41$datetime=date_create($date,$timezone);4243if(false===$datetime){44returnfalse;45}4647// Returns a sum of timestamp with timezone offset. Ideally should never be used.48if('G'===$format||'U'===$format){49return$datetime->getTimestamp()+$datetime->getOffset();50}5152if($translate){53returnwp_date($format,$datetime->getTimestamp(),$timezone);54}5556return$datetime->format($format);57}
History
Introduced in 0.71. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.