【问题标题】:Validate if the date entered (using HTML5 date picker) is older than today's date验证输入的日期(使用 HTML5 日期选择器)是否早于今天的日期
【发布时间】:2014-04-23 06:08:47
【问题描述】:
在 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
html
validation
date
【解决方案1】:
if (time() > strtotime($_POST['date'])) {
echo "The date entered must be greater than today's date";
} else {
echo "The date is valid!";
}
【解决方案2】:
<?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。
【解决方案3】:
这里你有正确的答案:
$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;