Jump to content

Subtracting two time stamps from one another


HughbertD

Recommended Posts

Fairly regular question I'm sure but can't seem to find what I am looking for elsewhere.

 

I have two time stamps in a MySQL DB, start and finish.

 

I want to find the amount of time between them in Days, Months, Year, Hours, Minutes, Seconds

 

I have the values stored as $start and $end

 

Thanks for any help you can give

You could take this approach:

 

$result = $end - $start;
$hours = floor($result / 3600);
$result -= $hours * 3600;
$mins = floor($result / 60);
$result -= $mins * 60;
$secs = $result;

print $hours . ' hours, ' . $mins . ' mins, ' . $secs . ' secs';

Should work, but I'd imagine there's a much easier way to do it...just can't think of it right now.

This method doesn't work for me

 

I do

$result = $end - $start

$conv=date("H \h\o\u\\r\s\ i \m\i\\n\s\ s \s\e\c\s",$result);

echo ($conv);

 

All I get it every result = 1 hour

 

If you actually read the whole post, you would have seen to use gmdate()

 

<?php
$result = $end - $start
$conv=gmdate("H \h\o\u\\r\s\ i \m\i\\n\s\ s \s\e\c\s",$result);
echo ($conv);
?>

 

<?php
$result = $end - $start
$conv=gmdate("H \h\o\u\\r\s\ i \m\i\\n\s\ s \s\e\c\s",$result);
echo ($conv);
?>

 

This outputs :

00 hours 00 mins 00 secs

 

For every result.

 

Sorry- I thought the gmdate was for timezone differences not just differences in time

what are the values for start and end?

 

Because

<?php
$start = strtotime("2/12/2008 1:15PM GMT");
$end = strtotime("2/13/2008 10:00AM GMT");
$result = $end - $start;
$conv=gmdate("H \h\o\u\\r\s\ i \m\i\\n\s\ s \s\e\c\s",$result);
print $conv;
?>

Outputs this for me: 20 hours 45 mins 00 secs

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.