我有一个表单来捕获日期格式的帖子输入字段,我需要检查此帖子字段是否为Y-m-d
格式,日期不在以后的任何日期。
$dob = $_POST['dob'];
// 1995-11-03 => Correct format
// 1995-30-12 => Incorrect format
// 2018-09-23 => Incorrect future date
答案 0 :(得分:2)
list($year, $month, $day) = explode('-', $_POST['dob']);
$timestamp = mktime(0, 0, 0, $month, $day, $year);
// Check if the date is valid, and that it's in the past
$isValid = checkdate($month, $day, $year) && $timestamp <= time();
手动:http://php.net/manual/en/function.checkdate.php和http://php.net/manual/en/function.mktime.php