rest_get_date_with_gmt

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

WordPress Version: 6.2

/**
 * Parses a date into both its local and UTC equivalent, in MySQL datetime format.
 *
 * @since 4.4.0
 *
 * @see rest_parse_date()
 *
 * @param string $date   RFC3339 timestamp.
 * @param bool   $is_utc Whether the provided date should be interpreted as UTC. Default false.
 * @return array|null {
 *     Local and UTC datetime strings, in MySQL datetime format (Y-m-d H:i:s),
 *     null on failure.
 *
 *     @type string $0 Local datetime string.
 *     @type string $1 UTC datetime string.
 * }
 */
function rest_get_date_with_gmt($date, $is_utc = false)
{
    /*
     * Whether or not the original date actually has a timezone string
     * changes the way we need to do timezone conversion.
     * Store this info before parsing the date, and use it later.
     */
    $has_timezone = preg_match('#(Z|[+-]\d{2}(:\d{2})?)$#', $date);
    $date = rest_parse_date($date);
    if (empty($date)) {
        return null;
    }
    /*
     * At this point $date could either be a local date (if we were passed
     * a *local* date without a timezone offset) or a UTC date (otherwise).
     * Timezone conversion needs to be handled differently between these two cases.
     */
    if (!$is_utc && !$has_timezone) {
        $local = gmdate('Y-m-d H:i:s', $date);
        $utc = get_gmt_from_date($local);
    } else {
        $utc = gmdate('Y-m-d H:i:s', $date);
        $local = get_date_from_gmt($utc);
    }
    return array($local, $utc);
}

WordPress Version: 5.4

/**
 * Parses a date into both its local and UTC equivalent, in MySQL datetime format.
 *
 * @since 4.4.0
 *
 * @see rest_parse_date()
 *
 * @param string $date   RFC3339 timestamp.
 * @param bool   $is_utc Whether the provided date should be interpreted as UTC. Default false.
 * @return array|null Local and UTC datetime strings, in MySQL datetime format (Y-m-d H:i:s),
 *                    null on failure.
 */
function rest_get_date_with_gmt($date, $is_utc = false)
{
    /*
     * Whether or not the original date actually has a timezone string
     * changes the way we need to do timezone conversion.
     * Store this info before parsing the date, and use it later.
     */
    $has_timezone = preg_match('#(Z|[+-]\d{2}(:\d{2})?)$#', $date);
    $date = rest_parse_date($date);
    if (empty($date)) {
        return null;
    }
    /*
     * At this point $date could either be a local date (if we were passed
     * a *local* date without a timezone offset) or a UTC date (otherwise).
     * Timezone conversion needs to be handled differently between these two cases.
     */
    if (!$is_utc && !$has_timezone) {
        $local = gmdate('Y-m-d H:i:s', $date);
        $utc = get_gmt_from_date($local);
    } else {
        $utc = gmdate('Y-m-d H:i:s', $date);
        $local = get_date_from_gmt($utc);
    }
    return array($local, $utc);
}

WordPress Version: 5.3

/**
 * Parses a date into both its local and UTC equivalent, in MySQL datetime format.
 *
 * @since 4.4.0
 *
 * @see rest_parse_date()
 *
 * @param string $date   RFC3339 timestamp.
 * @param bool   $is_utc Whether the provided date should be interpreted as UTC. Default false.
 * @return array|null Local and UTC datetime strings, in MySQL datetime format (Y-m-d H:i:s),
 *                    null on failure.
 */
function rest_get_date_with_gmt($date, $is_utc = false)
{
    // Whether or not the original date actually has a timezone string
    // changes the way we need to do timezone conversion.  Store this info
    // before parsing the date, and use it later.
    $has_timezone = preg_match('#(Z|[+-]\d{2}(:\d{2})?)$#', $date);
    $date = rest_parse_date($date);
    if (empty($date)) {
        return null;
    }
    // At this point $date could either be a local date (if we were passed a
    // *local* date without a timezone offset) or a UTC date (otherwise).
    // Timezone conversion needs to be handled differently between these two
    // cases.
    if (!$is_utc && !$has_timezone) {
        $local = gmdate('Y-m-d H:i:s', $date);
        $utc = get_gmt_from_date($local);
    } else {
        $utc = gmdate('Y-m-d H:i:s', $date);
        $local = get_date_from_gmt($utc);
    }
    return array($local, $utc);
}

WordPress Version: 7.3

/**
 * Parses a date into both its local and UTC equivalent, in MySQL datetime format.
 *
 * @since 4.4.0
 *
 * @see rest_parse_date()
 *
 * @param string $date   RFC3339 timestamp.
 * @param bool   $is_utc Whether the provided date should be interpreted as UTC. Default false.
 * @return array|null Local and UTC datetime strings, in MySQL datetime format (Y-m-d H:i:s),
 *                    null on failure.
 */
function rest_get_date_with_gmt($date, $is_utc = false)
{
    // Whether or not the original date actually has a timezone string
    // changes the way we need to do timezone conversion.  Store this info
    // before parsing the date, and use it later.
    $has_timezone = preg_match('#(Z|[+-]\d{2}(:\d{2})?)$#', $date);
    $date = rest_parse_date($date);
    if (empty($date)) {
        return null;
    }
    // At this point $date could either be a local date (if we were passed a
    // *local* date without a timezone offset) or a UTC date (otherwise).
    // Timezone conversion needs to be handled differently between these two
    // cases.
    if (!$is_utc && !$has_timezone) {
        $local = date('Y-m-d H:i:s', $date);
        $utc = get_gmt_from_date($local);
    } else {
        $utc = date('Y-m-d H:i:s', $date);
        $local = get_date_from_gmt($utc);
    }
    return array($local, $utc);
}

WordPress Version: 4.4

/**
 * Retrieves a local date with its GMT equivalent, in MySQL datetime format.
 *
 * @since 4.4.0
 *
 * @see rest_parse_date()
 *
 * @param string $date      RFC3339 timestamp.
 * @param bool   $force_utc Whether a UTC timestamp should be forced. Default false.
 * @return array|null Local and UTC datetime strings, in MySQL datetime format (Y-m-d H:i:s),
 *                    null on failure.
 */
function rest_get_date_with_gmt($date, $force_utc = false)
{
    $date = rest_parse_date($date, $force_utc);
    if (empty($date)) {
        return null;
    }
    $utc = date('Y-m-d H:i:s', $date);
    $local = get_date_from_gmt($utc);
    return array($local, $utc);
}