leon_nerd Posted May 29, 2011 Share Posted May 29, 2011 Hi, I am trying to get the difference in time in mins, hours etc thingy. Now, for some reason I cannot get the right minutes or hours difference between two time stamps. Here is my code: $postedTime=strtotime($comment_from_db->date_posted); $currentTime=time(); $mins=floor(($currentTime-$postedTime)/60); if($mins<=1){ echo $mins." minute ago by"; }elseif($mins>1){ if($mins>60){ $hrs=floor($mins/60); if($hrs>24){ }else{ if($hrs<2){ echo $hrs. " hour ago by"; }else{ echo $hrs. " hours ago by"; } } }else{ echo $mins." minutes ago by"; } } Now, for a comment posted like 25 mins ago it shows it was posted 4 hours ago. For a comment posted around 10 hours ago it shows it was posted 16 hours ago. What is the problem here? I was doing a test and checked the time in seconds after an interval of 2 mins. Here are the results: Comment posted: 1306669490 Current time: 1306683920 The difference is around 14430. This is not equal to 2 minutes. So, I am starting to think if the time I am getting is wrong? Should it have 10 digits? or less? I am not sure what is going on. Please, advice. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/237782-cannot-get-time-difference-right/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 29, 2011 Share Posted May 29, 2011 What does echoing $comment_from_db->date_posted show? What does echo date('Y-m-d H:i:s') show? And, strtotime() takes into account the current time zone setting, whereas time() does not, so you might have an extra hour or two (or four) being used in the math. Quote Link to comment https://forums.phpfreaks.com/topic/237782-cannot-get-time-difference-right/#findComment-1221909 Share on other sites More sharing options...
leon_nerd Posted May 29, 2011 Author Share Posted May 29, 2011 Echoing $comment_from_db->date_posted shows 2011-05-29 11:44:50 I was thinking about maybe the timezone is a problem. I am in GMT: -5. I will try to adjust GMT time in my php code and see if it works. Quote Link to comment https://forums.phpfreaks.com/topic/237782-cannot-get-time-difference-right/#findComment-1221911 Share on other sites More sharing options...
PFMaBiSmAd Posted May 29, 2011 Share Posted May 29, 2011 You should be able to directly get strtotime to use GMT - $postedTime=strtotime($comment_from_db->date_posted . ' GMT'); Quote Link to comment https://forums.phpfreaks.com/topic/237782-cannot-get-time-difference-right/#findComment-1221918 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.