4

I am using PHP, jQuery AJAX and HTML to create a timesheet system, for this the user needs to select 2 dates within 1 month of each other. The system as yet is working and shows (very limited) data.

BUT! When I actually select a date over the month limit (i.e. 2 months further than the start or another year after the start), it still shows the 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();
}

The dates are passed by a jQuery datepicker object via AJAX, and the dates I use, for example, are passed as such:

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

There is a check in place that sees if the dates given start before the other and this works, I have tried the same kind of thing for this check, but without success, this check is as such:

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
Dates 2 or more months apart within the same year work, tested again and this is the case, but dates into the next year do not

Can O' Spam
  • 2,718
  • 4
  • 19
  • 45
  • Did you verify that the two `DateTime` objects have the data you expect? – Powerlord Dec 14 '15 at 15:02
  • @Powerlord, when using `var_dump` of both DateTimeInterface objects and the `date_diff` of them, it gave me what I needed. – Can O' Spam Dec 14 '15 at 15:03
  • Possible duplicate of [How to calculate the difference between two dates using PHP?](https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Dinesh Ghule Jun 20 '18 at 10:54

2 Answers2

1

In your first part of the php code, you have put this operator>, but the problem is it means, everything Smaller than 1, not everything that is smaller than one or equal to 1. The easy solution is to change the operators to >=; which means everything that is equal to 1 or smaller than 1.

Jonas Hoffmann
  • 305
  • 1
  • 9
0

The date_diff constructs in PHP suck monkeyballs. Far more practical is to use straight comparisons instead:

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

Also please do not use $_REQUEST, it has potentially terrible security issues. You should use $_GET, $_POST or $_COOKIE according to what you explicitly expect.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136