我使用
PHP,jQuery AJAX和HTML来创建时间表系统,为此用户需要在1个月内选择2个日期.该系统尚未运行并显示(非常有限)数据.
但!当我实际选择超过月份限制的日期时(即比开始时间或开始后的另一年多2个月),它仍然显示包含数据的表格.
为此,我有这个检查:
$dt1 = new DateTime($_REQUEST['startdate']);
$dt2 = new DateTime($_REQUEST['enddate']);
$diff = date_diff($dt1, $dt2);
// I have tried this the other way around and get the same result...
if($diff->m > 1 || $diff->y > 1)
{
print("
Please choose another date or time within a month of each other
die();
}
日期由jQuery datepicker对象通过AJAX传递,例如,我使用的日期如下传递:
11/14/2015 (start date) && 12/14/2015 (end date) – should show data
09/14/2015 (start date) && 12/14/2015 (end date) – should not show data but does
11/14/2015 (start date) && 12/14/2016 (end date) – should not show data but does
有一个检查,看看给定的日期是否在另一个之前开始,这是有效的,我已经为此检查尝试了同样的事情,但没有成功,这个检查是这样的:
function CountDaysBetween($startDate, $endDate)
{
$begin = strtotime($startDate);
$end = strtotime($endDate);
if ($begin > $end) {
echo "start date is in the future!
";
return;
} else {
$no_days = 0;
$weekends = 0;
while ($begin <= $end) {
$no_days++; // no of days in the given interval
$what_day = date("N", $begin);
if ($what_day > 5) { // 6 and 7 are weekend days
$weekends++;
};
$begin += 86400; // +1 day
};
$working_days = $no_days - $weekends;
return $working_days + 1;
}
}
编辑
在同一年内相隔2个月或更长时间工作,再次进行测试,情况确实如此,但明年的日期不会