在PHP中查找2个日期之间的差异
发布时间:2020-12-13 15:59:38 所属栏目:PHP教程 来源:网络整理
导读:我使用 PHP,jQuery AJAX和HTML来创建时间表系统,为此用户需要在1个月内选择2个日期.该系统尚未运行并显示(非常有限)数据. 但!当我实际选择超过月份限制的日期时(即比开始时间或开始后的另一年多2个月),它仍然显示包含数据的表格. 为此,我有这个检查: $dt1
|
我使用
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传递,例如,我使用的日期如下传递:
有一个检查,看看给定的日期是否在另一个之前开始,这是有效的,我已经为此检查尝试了同样的事情,但没有成功,这个检查是这样的: 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;
}
}
编辑 解决方法
在你的PHP代码的第一部分,你已经把这个运算符>,但问题是它意味着,一切都小于1,而不是小于1或等于1的一切.简单的解决方案是将运算符更改为> ; =;这意味着所有等于1或小于1的东西.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
