exhaler Posted September 20, 2011 Share Posted September 20, 2011 hi all, i'm trying to calculate the time difference between two dates in hours, for example: 2010-12-13 15:26:56 and 2011-12-13 15:26:56 i tried mktime but wasn't able to get a working function. Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/247507-calculating-the-difference-between-two-dates/ Share on other sites More sharing options...
AyKay47 Posted September 20, 2011 Share Posted September 20, 2011 http://www.php.net/manual/en/datetime.diff.php might be viable here Quote Link to comment https://forums.phpfreaks.com/topic/247507-calculating-the-difference-between-two-dates/#findComment-1270987 Share on other sites More sharing options...
cssfreakie Posted September 20, 2011 Share Posted September 20, 2011 or just do: <?php $time1 = strtotime('2010-12-13 15:26:56'); $time2 = strtotime('2011-12-13 15:26:56'); echo $result = ($time2 - $time1)/3600; //60seconds * 60min ?> Quote Link to comment https://forums.phpfreaks.com/topic/247507-calculating-the-difference-between-two-dates/#findComment-1270998 Share on other sites More sharing options...
xyph Posted September 20, 2011 Share Posted September 20, 2011 These look like timestamps returned from a MySQL database? If so, do it in SQL, it's quite a bit faster. Generally, the best way to do this is to get the UNIX_TIMESTAMP of each date so you can get the difference in seconds, then use simple arithmetic to determine if you want to show how many hours/days/etc difference there is. SELECT UNIX_TIMESTAMP(`date1`) as `date1_stamp`, UNIX_TIMESTAMP(`date2`) as `date2_stamp` FROM `table` Quote Link to comment https://forums.phpfreaks.com/topic/247507-calculating-the-difference-between-two-dates/#findComment-1271077 Share on other sites More sharing options...
requinix Posted September 20, 2011 Share Posted September 20, 2011 Keep in mind the difference will be actual elapsed time, not what it "looks" like: the difference between 2011-03-13 12am and 2011-03-14 12am is 23 hours. Quote Link to comment https://forums.phpfreaks.com/topic/247507-calculating-the-difference-between-two-dates/#findComment-1271095 Share on other sites More sharing options...
AbraCadaver Posted September 20, 2011 Share Posted September 20, 2011 And if the dates cross one of the changes for daylight savings time, then you are off by 1 hour one way or the other. If using calculations then make sure that the timestamp is derived by the date at UTC or GMT or any timezone that doesn't observe daylight savings time. Quote Link to comment https://forums.phpfreaks.com/topic/247507-calculating-the-difference-between-two-dates/#findComment-1271099 Share on other sites More sharing options...
xyph Posted September 20, 2011 Share Posted September 20, 2011 It depends if you want actual hours passed, or hours passed on a clock/calendar. It's good to point out that potential quirk though. Quote Link to comment https://forums.phpfreaks.com/topic/247507-calculating-the-difference-between-two-dates/#findComment-1271102 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.