Uses wp_checkdate to return a valid Gregorian-calendar value for post_date.
Description
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.
Parameters
$post_datestringoptional
The date in mysql format (Y-m-d H:i:s).Default: ''
$post_date_gmtstringoptional
The GMT date in mysql format (Y-m-d H:i:s).Default: ''
Return
string|false
A valid Gregorian-calendar date string, or false on failure.
Uses · 3
current_time()Retrieves the current time based on specified type.
get_date_from_gmt()Given a date in UTC or GMT timezone, returns that date in the timezone of the site.
wp_checkdate()Tests if the supplied date is valid for the Gregorian calendar.
5424functionwp_resolve_post_date($post_date='',$post_date_gmt=''){5425// If the date is empty, set the date to now.5426if(empty($post_date)||'0000-00-00 00:00:00'===$post_date){5427if(empty($post_date_gmt)||'0000-00-00 00:00:00'===$post_date_gmt){5428$post_date=current_time('mysql');5429}else{5430$post_date=get_date_from_gmt($post_date_gmt);5431}5432}54335434// Validate the date.5435preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})/',$post_date,$matches);54365437if(empty($matches)||!is_array($matches)||count($matches)<4){5438returnfalse;5439}54405441$valid_date=wp_checkdate($matches[2],$matches[3],$matches[1],$post_date);54425443if(!$valid_date){5444returnfalse;5445}5446return$post_date;5447}
History
Introduced in 5.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.