Jump to content

Converting XX:XX:XX into timestamp


papaface

Recommended Posts

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

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.