-1

I want to find the difference between two times. I have only time stored in database. I don't have date stored. Note : Time is in string format.

Case 1:

 Starting time : 06:18:09
 Ending time :  06:18:45

Case 2:

Starting time : 06:03:35    
Ending time 06:18:04

Case 3: [This case isn't working ]

Starting time : 23:44:56    
Ending time : 00:45:43

I'm using this code to find in seconds.But not working for me.

$datetime1 = date_create($a);
  $datetime2 = date_create($b);
  $interval = date_diff($datetime1, $datetime2);
 $min=$interval->format('%s');

Any helps ?

Thanks!

JohnED
  • 3
  • 4
  • 1
    The solution is here: http://stackoverflow.com/questions/13928021/getting-time-difference-between-two-times-in-php – monsune Dec 28 '15 at 01:20
  • Yes! I tried your method. But for this Starting time : 23:48:02 Ending time : 00:15:12 doesn't work. Thank you! – JohnED Dec 28 '15 at 01:38

1 Answers1

0

Substring at the first colon to remove the label, then use the remaining time stamp for parsing and subtracting.

Use PHP's strtotime method to convert the times as strings into longs, then subtract the two to find the difference in seconds. You'll have a number at this point, which you can then do whatever with.

$datetime1 = substr($datetime1, strpos($datetime1, ":"));
$time1 = strtotime($datetime1);

You can then use the date method to convert the time difference into a new string for outputting.

See:

Dylan Wheeler
  • 6,928
  • 14
  • 56
  • 80