title: How to know number of Nights spent given night start and end times in PHP
body: <p>I have this method that should calculate the no of nights spent in parking given the</p>
  <ol>
  <li>Check-in date and time.</li>
  <li>Check-out date and time.</li>
  <li>Night Start time.</li>
  <li>Night end time.</li>
  </ol>
  <p>This is the far I have gotten and then I was like, what next?</p>
  <pre class="lang-php prettyprint-override"><code>
  public function getNightsSpent()  {
  
     $dateCheckin = new Carbon('2022-08-25 17:23:22');
          $dateCheckout = new Carbon('2022-08-27 10:35:18');
  
          $checkInTime = new Carbon($dateCheckin-&gt;format('H:i:s'));
          $checkOutTime = new Carbon($dateCheckout-&gt;format('H:i:s'));
  
  
          $nightStart = new Carbon('23:00:00');
          $nightEnd = new Carbon('06:00:00');
  
          $nightHours = $nightStart-&gt;diffInRealHours($nightEnd);
  
          $dayMinutesBeforeFirstNightStart = $nightStart-&gt;diffInMinutes($checkInTime);
          $dayMinutesAfterLastNightEnd = $checkOutTime-&gt;diffInMinutes($nightEnd);
  
          $dayMinutesInBothEnds = $dayMinutesBeforeFirstNightStart + $dayMinutesAfterLastNightEnd;
          $dayHoursInBothEnds = $dayMinutesInBothEnds/60;
  
          $totalHoursSpent = $dateCheckout-&gt;diffInHours($dateCheckin);
  
          return ($totalHoursSpent - $dayHoursInBothEnds)/$nightHours;
  }
  
  </code></pre>
  <blockquote>
  <p>Note: I have hard-coded dates and times for easier reference.</p>
  </blockquote>
  <p>For this am getting the answer as <code>1.8</code> but when I introduce a different no say check-in be <code>2022-08-20</code> it is giving me <code>8.8 nights</code> which is not correct.</p>
  <p>I am using Laravel 9 and PHP8.1</p>