我在预订系统方面遇到问题,我试图自定义。如果我选择当天,验证似乎不适用于当前日期和时间。
验证如下。
if (strtotime($str) < time()) {
但这并不允许我预订当前日期,即使时间超过当前时间,也不确定我是否应该在验证中添加+ 1或者什么。任何想法都会非常有用。
这是完整的功能。
public function _validate_date($str) {
if (strtotime($str) < time()) {
$this->form_validation->set_message('_validate_date', 'Date must be after today, you can only make future reservations!');
return FALSE;
} else {
return TRUE;
}
}
答案 0 :(得分:1)
尝试以下解决方案:
$datetime1 = new DateTime($str);
$datetime1->setTime(0, 0, 0);
$datetime2 = new DateTime();
$datetime2->setTime(0, 0, 0);
//get the diff.
$diff = $datetime2->diff($datetime1);
$days = (int) $diff->format("%R%a");
if ($days < 0) {
echo 'past days';
} elseif ($days == 0) {
echo 'today';
} else {
echo 'future days';
}
您可以在此处找到一个工作示例:https://3v4l.org/QQ4Pu
您使用新解决方案的功能:
public function _validate_date($str) {
$datetime1 = new DateTime($str);
$datetime1->setTime(0, 0, 0);
$datetime2 = new DateTime();
$datetime2->setTime(0, 0, 0);
//get the diff.
$diff = $datetime2->diff($datetime1);
$days = (int) $diff->format("%R%a");
if ($days <= 0) {
$this->form_validation->set_message('_validate_date', 'Date must be after today, you can only make future reservations!');
return false;
} else {
return true;
}
}
您的函数的一个工作示例:https://3v4l.org/HPTVF
答案 1 :(得分:0)
如果要检查,当前时间不是更高的当前日期,请尝试:
if (strtotime($str) < strtotime(date('Y-m-d 23:59:59')) {