papaface Posted April 24, 2010 Share Posted April 24, 2010 Hey, I am trying to work out the difference between two times that are entered into a form like this: 00:00:10 and 00:00:13 I want to work out the time difference between the two times. Can anyone point me in the direction of how to do this? I have tried looking at the strftime function but I just can't work out how to do it. I know how to do it with mktime: $start = mktime(0, 0, 3, 0, 0, 0);//hour,minute,seconds $end = mktime(0, 0, 10, 0, 0, 0);; echo "start: ". $start . " end: ".$end."<br />"; echo $end-$start; //7 seconds difference But I'm not able to receive the data in a way to put it into the mktime function without causing problems. Any help would be appreciated Link to comment https://forums.phpfreaks.com/topic/199582-converting-xxxxxx-into-timestamp/ Share on other sites More sharing options...
Mchl Posted April 24, 2010 Share Posted April 24, 2010 KISS rule $start = 0 * 3600 + 0 * 60 + 3;//hour,minute,seconds $end = 0 * 3600 + 0 * 60 + 10; echo "start: ". $start . " end: ".$end."<br />"; echo $end-$start; //7 seconds difference Link to comment https://forums.phpfreaks.com/topic/199582-converting-xxxxxx-into-timestamp/#findComment-1047577 Share on other sites More sharing options...
salathe Posted April 24, 2010 Share Posted April 24, 2010 You could parse the time strings using sscanf, do a little bit of arithmetic then find the difference. Something like: $start = '00:00:10'; $end = '00:00:13'; sscanf($start, '%02d:%02d:%02d', $hours, $minutes, $seconds); $sec_start = ($hours * 3600) + ($minutes * 60) + $seconds; sscanf($end, '%02d:%02d:%02d', $hours, $minutes, $seconds); $sec_end = ($hours * 3600) + ($minutes * 60) + $seconds; printf("Difference between %s and %s is %+d seconds.", $start, $end, $sec_end - $sec_start); Link to comment https://forums.phpfreaks.com/topic/199582-converting-xxxxxx-into-timestamp/#findComment-1047578 Share on other sites More sharing options...
papaface Posted April 24, 2010 Author Share Posted April 24, 2010 Thats great, thank you Link to comment https://forums.phpfreaks.com/topic/199582-converting-xxxxxx-into-timestamp/#findComment-1047579 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.