0

I am trying to calculate the time difference in hours between to times in format hh:mm. The problem is the data is in both 12 hour and 24 hour. eg. 23:55 - 00:10 , 12:10 - 01:02, 10:50 - 11:10 etc..

I tried the method given here PHP - hours difference (HH:MM format)

I need a function with all conditions to handle the 24h, 12h to calculate the time difference in hours.

Many thanks.

1 Answers1

0

The code in linked solution is fine except over midnight case. If you assume positive difference (start before end) - you may add 1 day (in seconds) when it appears to be negative. Just change return statement to:

return ($timeDiff > 0) ? $timeDiff : $timeDiff + 24*60*60;
shudder
  • 2,076
  • 2
  • 20
  • 21