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.
5520functionwp_resolve_post_date($post_date='',$post_date_gmt=''){5521// If the date is empty, set the date to now.5522if(empty($post_date)||'0000-00-00 00:00:00'===$post_date){5523if(empty($post_date_gmt)||'0000-00-00 00:00:00'===$post_date_gmt){5524$post_date=current_time('mysql');5525}else{5526$post_date=get_date_from_gmt($post_date_gmt);5527}5528}55295530// Validate the date.5531preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})/',$post_date,$matches);55325533if(empty($matches)||!is_array($matches)||count($matches)<4){5534returnfalse;5535}55365537$valid_date=wp_checkdate($matches[2],$matches[3],$matches[1],$post_date);55385539if(!$valid_date){5540returnfalse;5541}5542return$post_date;5543}
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 7.1.0 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.