mysql2date() WordPress Function

The mysql2date() function converts a date from MySQL format to a PHP date/time format. The function takes two parameters: the date to be converted and the desired output format. The output format can be either a string or an array. If a string is provided, the function will return a string in the specified format. If an array is provided, the function will return an array with the converted date/time values.

mysql2date( string $format, string $date, bool $translate = true ) #

Convert given MySQL date string into a different format.


Description

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


Top ↑

Parameters

$format

(string)(Required)Format of the date to return.

$date

(string)(Required)Date string to convert.

$translate

(bool)(Optional)Whether the return date should be translated.

Default value: true


Top ↑

Return

(string|int|false) Integer if $format is 'U' or 'G', string otherwise. False on failure.


Top ↑

Source

File: wp-includes/functions.php

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 );
}


Top ↑

Changelog

Changelog
VersionDescription
0.71Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More