wp_resolve_post_date

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

WordPress Version: 6.4

/**
 * Uses wp_checkdate to return a valid Gregorian-calendar value for post_date.
 * If post_date is not provided, this first checks post_date_gmt if provided,
 * then falls back to use the current time.
 *
 * For back-compat purposes in wp_insert_post, an empty post_date and an invalid
 * post_date_gmt will continue to return '1970-01-01 00:00:00' rather than false.
 *
 * @since 5.7.0
 *
 * @param string $post_date     The date in mysql format (`Y-m-d H:i:s`).
 * @param string $post_date_gmt The GMT date in mysql format (`Y-m-d H:i:s`).
 * @return string|false A valid Gregorian-calendar date string, or false on failure.
 */
function wp_resolve_post_date($post_date = '', $post_date_gmt = '')
{
    // If the date is empty, set the date to now.
    if (empty($post_date) || '0000-00-00 00:00:00' === $post_date) {
        if (empty($post_date_gmt) || '0000-00-00 00:00:00' === $post_date_gmt) {
            $post_date = current_time('mysql');
        } else {
            $post_date = get_date_from_gmt($post_date_gmt);
        }
    }
    // Validate the date.
    $month = (int) substr($post_date, 5, 2);
    $day = (int) substr($post_date, 8, 2);
    $year = (int) substr($post_date, 0, 4);
    $valid_date = wp_checkdate($month, $day, $year, $post_date);
    if (!$valid_date) {
        return false;
    }
    return $post_date;
}

WordPress Version: 6.1

/**
 * Uses wp_checkdate to return a valid Gregorian-calendar value for post_date.
 * If post_date is not provided, this first checks post_date_gmt if provided,
 * then falls back to use the current time.
 *
 * For back-compat purposes in wp_insert_post, an empty post_date and an invalid
 * post_date_gmt will continue to return '1970-01-01 00:00:00' rather than false.
 *
 * @since 5.7.0
 *
 * @param string $post_date     The date in mysql format.
 * @param string $post_date_gmt The GMT date in mysql format.
 * @return string|false A valid Gregorian-calendar date string, or false on failure.
 */
function wp_resolve_post_date($post_date = '', $post_date_gmt = '')
{
    // If the date is empty, set the date to now.
    if (empty($post_date) || '0000-00-00 00:00:00' === $post_date) {
        if (empty($post_date_gmt) || '0000-00-00 00:00:00' === $post_date_gmt) {
            $post_date = current_time('mysql');
        } else {
            $post_date = get_date_from_gmt($post_date_gmt);
        }
    }
    // Validate the date.
    $month = (int) substr($post_date, 5, 2);
    $day = (int) substr($post_date, 8, 2);
    $year = (int) substr($post_date, 0, 4);
    $valid_date = wp_checkdate($month, $day, $year, $post_date);
    if (!$valid_date) {
        return false;
    }
    return $post_date;
}

WordPress Version: 5.7

/**
 * Uses wp_checkdate to return a valid Gregorian-calendar value for post_date.
 * If post_date is not provided, this first checks post_date_gmt if provided,
 * then falls back to use the current time.
 *
 * For back-compat purposes in wp_insert_post, an empty post_date and an invalid
 * post_date_gmt will continue to return '1970-01-01 00:00:00' rather than false.
 *
 * @since 5.7.0
 *
 * @param string $post_date     The date in mysql format.
 * @param string $post_date_gmt The GMT date in mysql format.
 * @return string|false A valid Gregorian-calendar date string, or false on failure.
 */
function wp_resolve_post_date($post_date = '', $post_date_gmt = '')
{
    // If the date is empty, set the date to now.
    if (empty($post_date) || '0000-00-00 00:00:00' === $post_date) {
        if (empty($post_date_gmt) || '0000-00-00 00:00:00' === $post_date_gmt) {
            $post_date = current_time('mysql');
        } else {
            $post_date = get_date_from_gmt($post_date_gmt);
        }
    }
    // Validate the date.
    $month = substr($post_date, 5, 2);
    $day = substr($post_date, 8, 2);
    $year = substr($post_date, 0, 4);
    $valid_date = wp_checkdate($month, $day, $year, $post_date);
    if (!$valid_date) {
        return false;
    }
    return $post_date;
}