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.
30functionmysql2date($format,$date,$translate=true){31if(empty($date)){32returnfalse;33}3435$timezone=wp_timezone();36$datetime=date_create($date,$timezone);3738if(false===$datetime){39returnfalse;40}4142// Returns a sum of timestamp with timezone offset. Ideally should never be used.43if('G'===$format||'U'===$format){44return$datetime->getTimestamp()+$datetime->getOffset();45}4647if($translate){48returnwp_date($format,$datetime->getTimestamp(),$timezone);49}5051return$datetime->format($format);52}
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 6.7.7 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.