php 2个时间查询差几天,在PHP中查找2个日期之间的差异

我使用

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("

Time between dates it too great
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个月或更长时间工作,再次进行测试,情况确实如此,但明年的日期不会

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值