I have this method that should calculate the no of nights spent in parking given the
- Check-in date and time.
- Check-out date and time.
- Night Start time.
- Night end time.
This is the far I have gotten and then I was like, what next?
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->format('H:i:s'));
$checkOutTime = new Carbon($dateCheckout->format('H:i:s'));
$nightStart = new Carbon('23:00:00');
$nightEnd = new Carbon('06:00:00');
$nightHours = $nightStart->diffInRealHours($nightEnd);
$dayMinutesBeforeFirstNightStart = $nightStart->diffInMinutes($checkInTime);
$dayMinutesAfterLastNightEnd = $checkOutTime->diffInMinutes($nightEnd);
$dayMinutesInBothEnds = $dayMinutesBeforeFirstNightStart + $dayMinutesAfterLastNightEnd;
$dayHoursInBothEnds = $dayMinutesInBothEnds/60;
$totalHoursSpent = $dateCheckout->diffInHours($dateCheckin);
return ($totalHoursSpent - $dayHoursInBothEnds)/$nightHours;
}
Note: I have hard-coded dates and times for easier reference.
For this am getting the answer as 1.8 but when I introduce a different no say check-in be 2022-08-20 it is giving me 8.8 nights which is not correct.
I am using Laravel 9 and PHP8.1