get_gmt_from_date

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

WordPress Version: 6.2

/**
 * Given a date in the timezone of the site, returns that date in UTC.
 *
 * Requires and returns a date in the Y-m-d H:i:s format.
 * Return format can be overridden using the $format parameter.
 *
 * @since 1.2.0
 *
 * @param string $date_string The date to be converted, in the timezone of the site.
 * @param string $format      The format string for the returned date. Default 'Y-m-d H:i:s'.
 * @return string Formatted version of the date, in UTC.
 */
function get_gmt_from_date($date_string, $format = 'Y-m-d H:i:s')
{
    $datetime = date_create($date_string, wp_timezone());
    if (false === $datetime) {
        return gmdate($format, 0);
    }
    return $datetime->setTimezone(new DateTimeZone('UTC'))->format($format);
}

WordPress Version: 5.5

/**
 * Given a date in the timezone of the site, returns that date in UTC.
 *
 * Requires and returns a date in the Y-m-d H:i:s format.
 * Return format can be overridden using the $format parameter.
 *
 * @since 1.2.0
 *
 * @param string $string The date to be converted, in the timezone of the site.
 * @param string $format The format string for the returned date. Default 'Y-m-d H:i:s'.
 * @return string Formatted version of the date, in UTC.
 */
function get_gmt_from_date($string, $format = 'Y-m-d H:i:s')
{
    $datetime = date_create($string, wp_timezone());
    if (false === $datetime) {
        return gmdate($format, 0);
    }
    return $datetime->setTimezone(new DateTimeZone('UTC'))->format($format);
}

WordPress Version: 5.4

/**
 * Given a date in the timezone of the site, returns that date in UTC timezone.
 *
 * Requires and returns a date in the Y-m-d H:i:s format.
 * Return format can be overridden using the $format parameter.
 *
 * @since 1.2.0
 *
 * @param string $string The date to be converted, in the timezone of the site.
 * @param string $format The format string for the returned date. Default 'Y-m-d H:i:s'.
 * @return string Formatted version of the date, in UTC timezone.
 */
function get_gmt_from_date($string, $format = 'Y-m-d H:i:s')
{
    $datetime = date_create($string, wp_timezone());
    if (false === $datetime) {
        return gmdate($format, 0);
    }
    return $datetime->setTimezone(new DateTimeZone('UTC'))->format($format);
}

WordPress Version: 5.3

/**
 * Returns a date in the GMT equivalent.
 *
 * Requires and returns a date in the Y-m-d H:i:s format.
 * Return format can be overridden using the $format parameter.
 *
 * @since 1.2.0
 *
 * @param string $string The date to be converted.
 * @param string $format The format string for the returned date. Default 'Y-m-d H:i:s'.
 * @return string GMT version of the date provided.
 */
function get_gmt_from_date($string, $format = 'Y-m-d H:i:s')
{
    $datetime = date_create($string, wp_timezone());
    if (false === $datetime) {
        return gmdate($format, 0);
    }
    return $datetime->setTimezone(new DateTimeZone('UTC'))->format($format);
}

WordPress Version: 4.4

/**
 * Returns a date in the GMT equivalent.
 *
 * Requires and returns a date in the Y-m-d H:i:s format. If there is a
 * timezone_string available, the date is assumed to be in that timezone,
 * otherwise it simply subtracts the value of the 'gmt_offset' option. Return
 * format can be overridden using the $format parameter.
 *
 * @since 1.2.0
 *
 * @param string $string The date to be converted.
 * @param string $format The format string for the returned date (default is Y-m-d H:i:s)
 * @return string GMT version of the date provided.
 */
function get_gmt_from_date($string, $format = 'Y-m-d H:i:s')
{
    $tz = get_option('timezone_string');
    if ($tz) {
        $datetime = date_create($string, new DateTimeZone($tz));
        if (!$datetime) {
            return gmdate($format, 0);
        }
        $datetime->setTimezone(new DateTimeZone('UTC'));
        $string_gmt = $datetime->format($format);
    } else {
        if (!preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches)) {
            $datetime = strtotime($string);
            if (false === $datetime) {
                return gmdate($format, 0);
            }
            return gmdate($format, $datetime);
        }
        $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
        $string_gmt = gmdate($format, $string_time - get_option('gmt_offset') * HOUR_IN_SECONDS);
    }
    return $string_gmt;
}

WordPress Version: 4.1

/**
 * Returns a date in the GMT equivalent.
 *
 * Requires and returns a date in the Y-m-d H:i:s format. If there is a
 * timezone_string available, the date is assumed to be in that timezone,
 * otherwise it simply subtracts the value of the 'gmt_offset' option. Return
 * format can be overridden using the $format parameter.
 *
 * @since 1.2.0
 *
 * @param string $string The date to be converted.
 * @param string $format The format string for the returned date (default is Y-m-d H:i:s)
 * @return string GMT version of the date provided.
 */
function get_gmt_from_date($string, $format = 'Y-m-d H:i:s')
{
    $tz = get_option('timezone_string');
    if ($tz) {
        $datetime = date_create($string, new DateTimeZone($tz));
        if (!$datetime) {
            return gmdate($format, 0);
        }
        $datetime->setTimezone(new DateTimeZone('UTC'));
        $string_gmt = $datetime->format($format);
    } else {
        if (!preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches)) {
            return gmdate($format, 0);
        }
        $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
        $string_gmt = gmdate($format, $string_time - get_option('gmt_offset') * HOUR_IN_SECONDS);
    }
    return $string_gmt;
}

WordPress Version: 3.7

/**
 * Returns a date in the GMT equivalent.
 *
 * Requires and returns a date in the Y-m-d H:i:s format. If there is a
 * timezone_string available, the date is assumed to be in that timezone,
 * otherwise it simply subtracts the value of the 'gmt_offset' option. Return
 * format can be overridden using the $format parameter.
 *
 * @since 1.2.0
 *
 * @uses get_option() to retrieve the value of 'gmt_offset'.
 * @param string $string The date to be converted.
 * @param string $format The format string for the returned date (default is Y-m-d H:i:s)
 * @return string GMT version of the date provided.
 */
function get_gmt_from_date($string, $format = 'Y-m-d H:i:s')
{
    $tz = get_option('timezone_string');
    if ($tz) {
        $datetime = date_create($string, new DateTimeZone($tz));
        if (!$datetime) {
            return gmdate($format, 0);
        }
        $datetime->setTimezone(new DateTimeZone('UTC'));
        $string_gmt = $datetime->format($format);
    } else {
        if (!preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches)) {
            return gmdate($format, 0);
        }
        $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
        $string_gmt = gmdate($format, $string_time - get_option('gmt_offset') * HOUR_IN_SECONDS);
    }
    return $string_gmt;
}