HughbertD Posted February 13, 2008 Share Posted February 13, 2008 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 https://forums.phpfreaks.com/topic/90904-subtracting-two-time-stamps-from-one-another/ Share on other sites More sharing options...
thebadbad Posted February 13, 2008 Share Posted February 13, 2008 This thread will give you the answer. Link to comment https://forums.phpfreaks.com/topic/90904-subtracting-two-time-stamps-from-one-another/#findComment-465893 Share on other sites More sharing options...
HughbertD Posted February 13, 2008 Author Share Posted February 13, 2008 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 Link to comment https://forums.phpfreaks.com/topic/90904-subtracting-two-time-stamps-from-one-another/#findComment-465943 Share on other sites More sharing options...
Bauer418 Posted February 13, 2008 Share Posted February 13, 2008 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 https://forums.phpfreaks.com/topic/90904-subtracting-two-time-stamps-from-one-another/#findComment-465945 Share on other sites More sharing options...
rhodesa Posted February 13, 2008 Share Posted February 13, 2008 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 https://forums.phpfreaks.com/topic/90904-subtracting-two-time-stamps-from-one-another/#findComment-465955 Share on other sites More sharing options...
HughbertD Posted February 13, 2008 Author Share Posted February 13, 2008 <?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 Link to comment https://forums.phpfreaks.com/topic/90904-subtracting-two-time-stamps-from-one-another/#findComment-465960 Share on other sites More sharing options...
rhodesa Posted February 13, 2008 Share Posted February 13, 2008 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 https://forums.phpfreaks.com/topic/90904-subtracting-two-time-stamps-from-one-another/#findComment-465965 Share on other sites More sharing options...
HughbertD Posted February 13, 2008 Author Share Posted February 13, 2008 Ha I see - I wasn't putting strtotime($end) on the values Works fine now. Thank you very much. Slightly off topic - but where is the topic solved button? Link to comment https://forums.phpfreaks.com/topic/90904-subtracting-two-time-stamps-from-one-another/#findComment-465972 Share on other sites More sharing options...
rhodesa Posted February 13, 2008 Share Posted February 13, 2008 It disappeared a few days ago....I think the site admins are still working on fixing that. Link to comment https://forums.phpfreaks.com/topic/90904-subtracting-two-time-stamps-from-one-another/#findComment-465974 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.