在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!';
}
我该怎么办?
由于
答案 0 :(得分:1)
if (time() > strtotime($_POST['date'])) {
echo "The date entered must be greater than today's date";
} else {
echo "The date is valid!";
}
答案 1 :(得分:1)
<?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
。
答案 2 :(得分:1)
这里你有正确的答案:
$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;