WordPress at Your Fingertips

mysql2date() WP 0.71

Convert given date string into a different format.

  • $format should be a PHP date format string.
  • 'U' and 'G' formats will return an integer sum of timestamp with timezone offset.
  • $date is expected to be local time in MySQL format (Y-m-d H:i:s).

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() translation.

Uses: wp_date()
1 time — 0.00018 sec (fast) | 50000 times — 4.42 sec (fast)

No Hooks.


String|Int|false. Integer if $format is 'U' or 'G', string otherwise. False on failure.


mysql2date( $format, $date, $translate );
$format(string) (required)
Format of the date to return.
$date(string) (required)
Date string to convert.
Whether the return date should be translated.
Default: true


Since 0.71 Introduced.

Code of mysql2date() WP 5.9

function mysql2date( $format, $date, $translate = true ) {
	if ( empty( $date ) ) {
		return false;

	$datetime = date_create( $date, wp_timezone() );

	if ( false === $datetime ) {
		return false;

	// Returns a sum of timestamp with timezone offset. Ideally should never be used.
	if ( 'G' === $format || 'U' === $format ) {
		return $datetime->getTimestamp() + $datetime->getOffset();

	if ( $translate ) {
		return wp_date( $format, $datetime->getTimestamp() );

	return $datetime->format( $format );

date time

No comments
    Log In