在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("<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的一切.简单的解决方案是将运算符更改为&gt ; =;这意味着所有等于1或小于1的东西.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...