Well, let's assume that you have two strings in HH:MM format; $time1
and $time2
say. I'm going to assume that $time1
and $time2
refer to times on the same day in the same time zone (unless $time1
is after $time2
, in which case I'll assume that $time2
is the day after $time1
).
$time1 = explode(':', $time1);
$time2 = explode(':', $time2);
$time1_hours = intval($time1[0], 10);
$time1_mins = intval($time1[1], 10);
$time2_hours = intval($time2[0], 10);
$time2_mins = intval($time2[1], 10);
$time1_mins_past_midnight = 60 * $time1_hours + $time1_mins;
$time2_mins_past_midnight = 60 * $time2_hours + $time2_mins;
$time_difference = ( $time1_mins_past_midnight > $time2_mins_past_midnight ) ?
1440 + $time2_mins_past_midnight - $time1_mins_past_midnight :
$time2_mins_past_midnight - $time1_mins_past_midnight;
Now $time_difference
contains the time difference, in minutes.