检查日期是否为'Y-m-d'格式且未来的日期不是?

时间:2018-05-08 03:12:10

标签: php validation date

我有一个表单来捕获日期格式的帖子输入字段,我需要检查此帖子字段是否为Y-m-d格式,日期不在以后的任何日期。

$dob = $_POST['dob']; 
// 1995-11-03 => Correct format
// 1995-30-12 => Incorrect format
// 2018-09-23 => Incorrect future date

1 个答案:

答案 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.phphttp://php.net/manual/en/function.mktime.php

相关问题