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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.