Finding the difference between two dates in PHP

I use PHP, jQuery AJAX and HTML to create a schedule system, for this the user needs to select 2 dates within 1 month from each other. The system is still working and shows (very limited) data.

BUT! When I actually choose a date for the month limit (i.e. 2 months further than at the beginning or another year after the start), it still shows a table with the data.

For this, I have this check:

$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();
}

Dates are passed by the jQuery dating object via AJAX, and the dates that I use, for example, are passed as such:

11/14/2015 ( ) && & 12/14/2015 ( ) -
14.09.2015 ( ) && 12/14/2015 ( ) - , 11/14/2015 ( ) && 12/14/2016 ( ) - ,

, , , , , , ​​ , :

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;
    }
}

Edit
2 , , ,

+4
2

PHP- >, , , 1, , 1. >=; , 1 1.

+1

date_diff PHP suck monkeyballs. :

$dt1 = new \DateTime($_REQUEST['startdate']);
$dt2 = new \DateTime($_REQUEST['enddate']);
$dt1->add(new \DateInterval('P1M'));
echo ($dt1 < $dt2 ? 'Less' : 'More') . ' than a month';

, $_REQUEST, . $_GET, $_POST $_COOKIE , .

0

Source: https://habr.com/ru/post/1619918/


All Articles