I have a reservation system, which charges a daily rate for overnights. I'm trying to figure out how to calculate the number of times midnight has been crossed for a given date range in PHP.
I have 2 unix timestamps:
$start_date = 1404349200; // 07/02/14 07:00:00 pm
$end_date = 1404936000; // 07/09/14 02:00:00 pm
$total_time_here = $end_date - $start_date; // number of seconds here
$number_overnights = (int) ($total_time_here / 60 / 60 / 24); // calculate number of 24-hour periods
$number_overnights
evaluates to 6
, however the number of times midnight occurred for that date range is 7
, which is what I want to count. This happens when the $start_date
happens later in the day than the $end_date
(7:00pm vs 2:00pm).
Both the $start_date
and $end_date
are in the same time zone, and are stored in UTC in the database and then converted back in PHP.
Thanks for your help!