tmyonline Posted May 20, 2009 Share Posted May 20, 2009 Hi guys, I need to calculate the time difference between t1 and t2, where t1 = 4/2/2009 16:17, and t2 = 4/27/2009 14:29 I did the following: $t1 = strtotime('4/2/2009 16:17'); $t2 = strtotime('4/27/2009 14:29'); the delta_T = (t2 - t1) will give me a time stamp. I would assume that this time stamp is expressed in seconds but it does not look correct in my calculation. So, I'm wondering if the time difference can be calculated this way using the strtotime(), and if it is the way, is the unit of the time difference (or time stamp) is expressed in seconds ? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/158980-calculate-time-difference-in-php/ Share on other sites More sharing options...
Prismatic Posted May 20, 2009 Share Posted May 20, 2009 <?php $t1 = strtotime('4/2/2009 16:17'); $t2 = strtotime('4/27/2009 14:29'); $delta_T = ($t2 - $t1); $days = round(($delta_T % 604800) / 86400, 2); $hours = round((($delta_T % 604800) % 86400) / 3600, 2); $minutes = round(((($delta_T % 604800) % 86400) % 3600) / 60, 2); $seconds = round((((($delta_T % 604800) % 86400) % 3600) % 60), 2); ?> One way. Quote Link to comment https://forums.phpfreaks.com/topic/158980-calculate-time-difference-in-php/#findComment-838457 Share on other sites More sharing options...
tmyonline Posted May 21, 2009 Author Share Posted May 21, 2009 Thanks Prismatic. Would you please explain to me as to why $hours = round((($delta_T % 604800) % 86400) / 3600, 2); ? In other words, I don't understand why I have to do a modulo (delta_T % 604800) and I have to do it again with 86400. I understand that there are 86,400 seconds in a day and 604,800 seconds in a week but I don't know the meaning of the above operations. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/158980-calculate-time-difference-in-php/#findComment-838612 Share on other sites More sharing options...
tmyonline Posted May 26, 2009 Author Share Posted May 26, 2009 Hi guys, I don't understand the time calculation given by Prismatic. I have waited for an answer for a week !!! Anyway, I would greatly appreciate any response. Prismatic's formula does give me reasonable answers in terms of the numbers. However, I wish to be able to interpret the meaning of that formula. Any ideas ? Thanks so much. Quote Link to comment https://forums.phpfreaks.com/topic/158980-calculate-time-difference-in-php/#findComment-842552 Share on other sites More sharing options...
Maq Posted May 26, 2009 Share Posted May 26, 2009 Try Googling, here's a tutorial - http://www.phpf1.com/tutorial/php-date-difference.html Quote Link to comment https://forums.phpfreaks.com/topic/158980-calculate-time-difference-in-php/#findComment-842682 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.