title: Total the number of days between two dates broken up by the end of year
body: <p>I'm working on a record keeping system for animals and I have to work out the number of days that the carer looks after one or more animals in any given year. </p>
  
  <p>Sometimes the date rolls over into the next year and I have to work out the number of days spent caring for them in year 1 and also the number of days in year two.</p>
  
  <p>I have put together some code that works except that it won't take the start date as day 1, its ignored.</p>
  
  <pre><code>&lt;?php
  date_default_timezone_set('Australia/Sydney');
  $start_date = new DateTime('2020-01-01');
  $end_date = new DateTime('2021-01-01');
  $index = 0;
  $period = new DatePeriod($start_date, new DateInterval('P1D'), $end_date);
  $oldYear = $start_date-&gt;format('Y');
  $name = 'Komala';
  $lastYear = 0;
  
  echo '&lt;table style="border-style: solid"&gt;';
      echo '&lt;tr&gt;&lt;td colspan="2"&gt;The number of days spent looking after '.$name.'&lt;/td&gt;';
          foreach($period as $key=&gt;$date) {
              $yearCheck = $date-&gt;format('Y');
              if ($yearCheck === $oldYear){
                  echo '';
              }else{
                  $lastYear = $index;
                  echo '&lt;td colspan="2"&gt;';
                  echo 'in '. $oldYear .' is: ' . ($lastYear);
                  echo '&lt;/td&gt;';
              }
              $index++;
              $oldYear = $yearCheck;
          }
          echo '&lt;td&gt;';
              echo 'in '. $oldYear .' is: ' . ($index - $lastYear);
          echo '&lt;/td&gt;&lt;td&gt;';
              echo 'For a total of '. $index .' days.';
      echo '&lt;/td&gt;&lt;/tr&gt;;';
  echo '&lt;/table&gt;';
  ?&gt;
  </code></pre>
  
  <p>When I use the date range as above from 01 Jan 2020 to 02 Jan 2021 it should say 368 days but instead it says 367.</p>
  
  <p><a href="https://i.stack.imgur.com/O31rM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/O31rM.png" alt="Code output"></a></p>
  
  <p>I tried to use <a href="https://www.php.net/manual/en/class.dateperiod.php#dateperiod.props.include_start_date" rel="nofollow noreferrer">DatePeriod::</a> to try to include the start date but I can't seem to make it work.</p>
  
  <p>Can someone help please?</p>