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.
5423functionwp_resolve_post_date($post_date='',$post_date_gmt=''){5424// If the date is empty, set the date to now.5425if(empty($post_date)||'0000-00-00 00:00:00'===$post_date){5426if(empty($post_date_gmt)||'0000-00-00 00:00:00'===$post_date_gmt){5427$post_date=current_time('mysql');5428}else{5429$post_date=get_date_from_gmt($post_date_gmt);5430}5431}54325433// Validate the date.5434preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})/',$post_date,$matches);54355436if(empty($matches)||!is_array($matches)||count($matches)<4){5437returnfalse;5438}54395440$valid_date=wp_checkdate($matches[2],$matches[3],$matches[1],$post_date);54415442if(!$valid_date){5443returnfalse;5444}5445return$post_date;5446}
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.0.4 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.