mysql2date

The timeline below displays how wordpress function mysql2date has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Converts given MySQL 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()` for translation.
 *
 * @since 0.71
 *
 * @param string $format    Format of the date to return.
 * @param string $date      Date string to convert.
 * @param bool   $translate Whether the return date should be translated. Default true.
 * @return string|int|false Integer if `$format` is 'U' or 'G', string otherwise.
 *                          False on failure.
 */
function mysql2date($format, $date, $translate = true)
{
    if (empty($date)) {
        return false;
    }
    $timezone = wp_timezone();
    $datetime = date_create($date, $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(), $timezone);
    }
    return $datetime->format($format);
}

WordPress Version: 6.1

/**
 * Converts given MySQL 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()` for translation.
 *
 * @since 0.71
 *
 * @param string $format    Format of the date to return.
 * @param string $date      Date string to convert.
 * @param bool   $translate Whether the return date should be translated. Default true.
 * @return string|int|false Integer if `$format` is 'U' or 'G', string otherwise.
 *                          False on failure.
 */
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);
}

WordPress Version: 5.9

/**
 * Convert given MySQL 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()` for translation.
 *
 * @since 0.71
 *
 * @param string $format    Format of the date to return.
 * @param string $date      Date string to convert.
 * @param bool   $translate Whether the return date should be translated. Default true.
 * @return string|int|false Integer if `$format` is 'U' or 'G', string otherwise.
 *                          False on failure.
 */
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);
}

WordPress Version: 5.3

/**
 * Convert given MySQL date string into a different format.
 *
 * `$format` should be a PHP date format string.
 * 'U' and 'G' formats will return a 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.
 *
 * @since 0.71
 *
 * @param string $format    Format of the date to return.
 * @param string $date      Date string to convert.
 * @param bool   $translate Whether the return date should be translated. Default true.
 * @return string|int|false Formatted date string or sum of Unix timestamp and timezone offset.
 *                          False on failure.
 */
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);
}

WordPress Version: 4.0

/**
 * Convert given date string into a different format.
 *
 * $format should be either a PHP date format string, e.g. 'U' for a Unix
 * timestamp, or 'G' for a Unix timestamp assuming that $date is GMT.
 *
 * If $translate is true then the given date and format string will
 * be passed to date_i18n() for translation.
 *
 * @since 0.71
 *
 * @param string $format    Format of the date to return.
 * @param string $date      Date string to convert.
 * @param bool   $translate Whether the return date should be translated. Default true.
 * @return string|int|bool Formatted date string or Unix timestamp. False if $date is empty.
 */
function mysql2date($format, $date, $translate = true)
{
    if (empty($date)) {
        return false;
    }
    if ('G' == $format) {
        return strtotime($date . ' +0000');
    }
    $i = strtotime($date);
    if ('U' == $format) {
        return $i;
    }
    if ($translate) {
        return date_i18n($format, $i);
    } else {
        return date($format, $i);
    }
}

WordPress Version: 3.7

/**
 * Converts given date string into a different format.
 *
 * $format should be either a PHP date format string, e.g. 'U' for a Unix
 * timestamp, or 'G' for a Unix timestamp assuming that $date is GMT.
 *
 * If $translate is true then the given date and format string will
 * be passed to date_i18n() for translation.
 *
 * @since 0.71
 *
 * @param string $format Format of the date to return.
 * @param string $date Date string to convert.
 * @param bool $translate Whether the return date should be translated. Default is true.
 * @return string|int Formatted date string, or Unix timestamp.
 */
function mysql2date($format, $date, $translate = true)
{
    if (empty($date)) {
        return false;
    }
    if ('G' == $format) {
        return strtotime($date . ' +0000');
    }
    $i = strtotime($date);
    if ('U' == $format) {
        return $i;
    }
    if ($translate) {
        return date_i18n($format, $i);
    } else {
        return date($format, $i);
    }
}