我使用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("<center><strong>Time between dates it too great<br />Please choose another date or time within a month of each other</strong></center>");
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! <br />";
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个月或更长时间工作,再次进行测试,情况确实如此,但明年的日期不会
解决方法:
在你的PHP代码的第一部分,你已经把这个运算符>,但问题是它意味着,一切都小于1,而不是小于1或等于1的一切.简单的解决方案是将运算符更改为> ; =;这意味着所有等于1或小于1的东西.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。