iso8601_to_datetime

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

WordPress Version: 5.7

/**
 * Given an ISO 8601 (Ymd\TH:i:sO) date, returns a MySQL DateTime (Y-m-d H:i:s) format used by post_date[_gmt].
 *
 * @since 1.5.0
 *
 * @param string $date_string Date and time in ISO 8601 format {@link https://en.wikipedia.org/wiki/ISO_8601}.
 * @param string $timezone    Optional. If set to 'gmt' returns the result in UTC. Default 'user'.
 * @return string|false The date and time in MySQL DateTime format - Y-m-d H:i:s, or false on failure.
 */
function iso8601_to_datetime($date_string, $timezone = 'user')
{
    $timezone = strtolower($timezone);
    $wp_timezone = wp_timezone();
    $datetime = date_create($date_string, $wp_timezone);
    // Timezone is ignored if input has one.
    if (false === $datetime) {
        return false;
    }
    if ('gmt' === $timezone) {
        return $datetime->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i:s');
    }
    if ('user' === $timezone) {
        return $datetime->setTimezone($wp_timezone)->format('Y-m-d H:i:s');
    }
    return false;
}

WordPress Version: 5.5

/**
 * Given an ISO 8601 (Ymd\TH:i:sO) date, returns a MySQL DateTime (Y-m-d H:i:s) format used by post_date[_gmt].
 *
 * @since 1.5.0
 *
 * @param string $date_string Date and time in ISO 8601 format {@link https://en.wikipedia.org/wiki/ISO_8601}.
 * @param string $timezone    Optional. If set to 'gmt' returns the result in UTC. Default 'user'.
 * @return string|bool The date and time in MySQL DateTime format - Y-m-d H:i:s, or false on failure.
 */
function iso8601_to_datetime($date_string, $timezone = 'user')
{
    $timezone = strtolower($timezone);
    $wp_timezone = wp_timezone();
    $datetime = date_create($date_string, $wp_timezone);
    // Timezone is ignored if input has one.
    if (false === $datetime) {
        return false;
    }
    if ('gmt' === $timezone) {
        return $datetime->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i:s');
    }
    if ('user' === $timezone) {
        return $datetime->setTimezone($wp_timezone)->format('Y-m-d H:i:s');
    }
    return false;
}

WordPress Version: 5.3

/**
 * Converts an iso8601 (Ymd\TH:i:sO) date to MySQL DateTime (Y-m-d H:i:s) format used by post_date[_gmt].
 *
 * @since 1.5.0
 *
 * @param string $date_string Date and time in ISO 8601 format {@link https://en.wikipedia.org/wiki/ISO_8601}.
 * @param string $timezone    Optional. If set to 'gmt' returns the result in UTC. Default 'user'.
 * @return string|bool The date and time in MySQL DateTime format - Y-m-d H:i:s, or false on failure.
 */
function iso8601_to_datetime($date_string, $timezone = 'user')
{
    $timezone = strtolower($timezone);
    $wp_timezone = wp_timezone();
    $datetime = date_create($date_string, $wp_timezone);
    // Timezone is ignored if input has one.
    if (false === $datetime) {
        return false;
    }
    if ('gmt' === $timezone) {
        return $datetime->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i:s');
    }
    if ('user' === $timezone) {
        return $datetime->setTimezone($wp_timezone)->format('Y-m-d H:i:s');
    }
    return false;
}

WordPress Version: 4.6

/**
 * Converts an iso8601 date to MySQL DateTime format used by post_date[_gmt].
 *
 * @since 1.5.0
 *
 * @param string $date_string Date and time in ISO 8601 format {@link https://en.wikipedia.org/wiki/ISO_8601}.
 * @param string $timezone    Optional. If set to GMT returns the time minus gmt_offset. Default is 'user'.
 * @return string The date and time in MySQL DateTime format - Y-m-d H:i:s.
 */
function iso8601_to_datetime($date_string, $timezone = 'user')
{
    $timezone = strtolower($timezone);
    if ($timezone == 'gmt') {
        preg_match('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', $date_string, $date_bits);
        if (!empty($date_bits[7])) {
            // we have a timezone, so let's compute an offset
            $offset = iso8601_timezone_to_offset($date_bits[7]);
        } else {
            // we don't have a timezone, so we assume user local timezone (not server's!)
            $offset = HOUR_IN_SECONDS * get_option('gmt_offset');
        }
        $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
        $timestamp -= $offset;
        return gmdate('Y-m-d H:i:s', $timestamp);
    } elseif ($timezone == 'user') {
        return preg_replace('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', '$1-$2-$3 $4:$5:$6', $date_string);
    }
}

WordPress Version: 4.3

/**
 * Converts an iso8601 date to MySQL DateTime format used by post_date[_gmt].
 *
 * @since 1.5.0
 *
 * @param string $date_string Date and time in ISO 8601 format {@link http://en.wikipedia.org/wiki/ISO_8601}.
 * @param string $timezone    Optional. If set to GMT returns the time minus gmt_offset. Default is 'user'.
 * @return string The date and time in MySQL DateTime format - Y-m-d H:i:s.
 */
function iso8601_to_datetime($date_string, $timezone = 'user')
{
    $timezone = strtolower($timezone);
    if ($timezone == 'gmt') {
        preg_match('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', $date_string, $date_bits);
        if (!empty($date_bits[7])) {
            // we have a timezone, so let's compute an offset
            $offset = iso8601_timezone_to_offset($date_bits[7]);
        } else {
            // we don't have a timezone, so we assume user local timezone (not server's!)
            $offset = HOUR_IN_SECONDS * get_option('gmt_offset');
        }
        $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
        $timestamp -= $offset;
        return gmdate('Y-m-d H:i:s', $timestamp);
    } elseif ($timezone == 'user') {
        return preg_replace('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', '$1-$2-$3 $4:$5:$6', $date_string);
    }
}

WordPress Version: 4.2

/**
 * Converts an iso8601 date to MySQL DateTime format used by post_date[_gmt].
 *
 * @since 1.5.0
 *
 * @param string $date_string Date and time in ISO 8601 format {@link http://en.wikipedia.org/wiki/ISO_8601}.
 * @param string $timezone Optional. If set to GMT returns the time minus gmt_offset. Default is 'user'.
 * @return string The date and time in MySQL DateTime format - Y-m-d H:i:s.
 */
function iso8601_to_datetime($date_string, $timezone = 'user')
{
    $timezone = strtolower($timezone);
    if ($timezone == 'gmt') {
        preg_match('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', $date_string, $date_bits);
        if (!empty($date_bits[7])) {
            // we have a timezone, so let's compute an offset
            $offset = iso8601_timezone_to_offset($date_bits[7]);
        } else {
            // we don't have a timezone, so we assume user local timezone (not server's!)
            $offset = HOUR_IN_SECONDS * get_option('gmt_offset');
        }
        $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
        $timestamp -= $offset;
        return gmdate('Y-m-d H:i:s', $timestamp);
    } elseif ($timezone == 'user') {
        return preg_replace('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', '$1-$2-$3 $4:$5:$6', $date_string);
    }
}

WordPress Version: 3.7

/**
 * Converts an iso8601 date to MySQL DateTime format used by post_date[_gmt].
 *
 * @since 1.5.0
 *
 * @param string $date_string Date and time in ISO 8601 format {@link http://en.wikipedia.org/wiki/ISO_8601}.
 * @param string $timezone Optional. If set to GMT returns the time minus gmt_offset. Default is 'user'.
 * @return string The date and time in MySQL DateTime format - Y-m-d H:i:s.
 */
function iso8601_to_datetime($date_string, $timezone = 'user')
{
    $timezone = strtolower($timezone);
    if ($timezone == 'gmt') {
        preg_match('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', $date_string, $date_bits);
        if (!empty($date_bits[7])) {
            // we have a timezone, so let's compute an offset
            $offset = iso8601_timezone_to_offset($date_bits[7]);
        } else {
            // we don't have a timezone, so we assume user local timezone (not server's!)
            $offset = HOUR_IN_SECONDS * get_option('gmt_offset');
        }
        $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
        $timestamp -= $offset;
        return gmdate('Y-m-d H:i:s', $timestamp);
    } else if ($timezone == 'user') {
        return preg_replace('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', '$1-$2-$3 $4:$5:$6', $date_string);
    }
}