验证输入的日期(使用HTML5日期选择器)是否比今天的日期早


Validate if the date entered (using HTML5 date picker) is older than today's date

在HTML中,我使用新的HTML 5日期选择器输入日期(mm/dd/yyyy):

 <input name="date" type="date">

我想验证输入的日期是否比今天的日期早

$this_date = $_POST['date'];
if ($this_date is older the today's date) {
    echo 'The date entered must be greater than today's date';
} else {
    echo 'The date is valid!';
}

我该怎么走?

谢谢

<?php
   $this_date = $_POST['date'];
   if (strtotime($this_date . " 00:00:00") > strtotime(date("m/d/Y 00:00:00"))) {
       echo "The date entered must be greater than today's date";
   } else {
       echo "The date is valid!";
    }
?>

此外,您应该使用preg_match()或其他技术检查用户输入的有效性。

请注意PHP将处理无效日期,如02/30/2014。计算结果为03/02/2014

if (time() > strtotime($_POST['date'])) {
    echo "The date entered must be greater than today's date";
} else {
    echo "The date is valid!";
}

正确答案:

$diff = strtotime($_POST['date'])>0? strtotime('Today')-strtotime($_POST['date']):NULL;
$res = $diff>0?"The date is valid!":"The date entered must be greater than today's date";
echo $res;