title: Finding the difference between 2 dates in PHP
body: <p>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.</p>
  
  <p><strong>BUT!</strong> 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.</p>
  
  <p>For this I have this check:</p>
  
  <pre><code>$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-&gt;m &gt; 1 || $diff-&gt;y &gt; 1)
  {
      print("&lt;center&gt;&lt;strong&gt;Time between dates it too great&lt;br /&gt;Please choose another date or time within a month of each other&lt;/strong&gt;&lt;/center&gt;");
      die();
  }
  </code></pre>
  
  <p>The dates are passed by a jQuery datepicker object via AJAX, and the dates I use, for example, are passed as such:</p>
  
  <blockquote>
    <p>11/14/2015 (start date) &amp;&amp; 12/14/2015 (end date) - should show data<br>
    09/14/2015 (start date) &amp;&amp; 12/14/2015 (end date) - should not show data but does<br>
    11/14/2015 (start date) &amp;&amp; 12/14/2016 (end date) - should not show data but does</p>
  </blockquote>
  
  <p>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:</p>
  
  <pre><code>function CountDaysBetween($startDate, $endDate)
  {
      $begin = strtotime($startDate);
      $end   = strtotime($endDate);
      if ($begin &gt; $end) {
          echo "start date is in the future! &lt;br /&gt;";
          return;
      } else {
          $no_days  = 0;
          $weekends = 0;
          while ($begin &lt;= $end) {
              $no_days++; // no of days in the given interval
              $what_day = date("N", $begin);
              if ($what_day &gt; 5) { // 6 and 7 are weekend days
                  $weekends++;
              };
              $begin += 86400; // +1 day
          };
          $working_days = $no_days - $weekends;
  
          return $working_days + 1;
      }
  }
  </code></pre>
  
  <p><strong>Edit</strong><br>
  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</p>