lynxus Posted August 28, 2009 Share Posted August 28, 2009 Hi guys, How can i echo the amount of time passed between a database timestamp and the current time. IE: select time from table limit 1; = 2009-08-28 09:55:45 How can i do: Time since last time = XXHours, XXminutes, XXSeconds. IE: the time between the above time and the current server time if this makes any sense? Thanks in advance. Graham Quote Link to comment https://forums.phpfreaks.com/topic/172256-solved-find-time-between-curent-time-and-a-timestamp-in-a-db/ Share on other sites More sharing options...
ignace Posted August 28, 2009 Share Posted August 28, 2009 SELECT * FROM table WHERE time1 BETWEEN time2 AND now() Quote Link to comment https://forums.phpfreaks.com/topic/172256-solved-find-time-between-curent-time-and-a-timestamp-in-a-db/#findComment-908219 Share on other sites More sharing options...
lynxus Posted August 28, 2009 Author Share Posted August 28, 2009 Hi, Thanks for your reply. Im confused? How is that going to tell me how much time has passed since the timestamp? I probably didnt make my question clear enough. Lets say $lasttime = "2009-08-28 09:55:45"; How can i say. XDays, Xhours, Xseconds has passed since $lasttime; Thanks G Quote Link to comment https://forums.phpfreaks.com/topic/172256-solved-find-time-between-curent-time-and-a-timestamp-in-a-db/#findComment-908221 Share on other sites More sharing options...
ignace Posted August 28, 2009 Share Posted August 28, 2009 $diff = time() - $lasttime; print strftime('%e days %H hours %M minutes %S seconds', $diff); Quote Link to comment https://forums.phpfreaks.com/topic/172256-solved-find-time-between-curent-time-and-a-timestamp-in-a-db/#findComment-908228 Share on other sites More sharing options...
lynxus Posted August 28, 2009 Author Share Posted August 28, 2009 Humm, getting closer this is what i have from my DB result: $result = mysql_query("SELECT time FROM visitorips where siteid = '$siteid' order by id DESC limit 1"); while($row = mysql_fetch_array($result)) { $diff = time() - $row['time']; $lasttime = strftime('%e days %H hours %M minutes %S seconds', $diff); } ( Result = 2009-08-28 11:12:47 ) However when i echo $lasttime i get: 28 days 10 hours 50 minutes 51 seconds Something must be wrong here? but im not sure what. the system time is correct as the time inserted to the DB is from the system time.. I would of thought it would say something like 0days 12minutes xx seconds ? Any ideas? Thanks Graham Quote Link to comment https://forums.phpfreaks.com/topic/172256-solved-find-time-between-curent-time-and-a-timestamp-in-a-db/#findComment-908241 Share on other sites More sharing options...
lynxus Posted August 28, 2009 Author Share Posted August 28, 2009 Anyone else have any ideas? Thanks G Quote Link to comment https://forums.phpfreaks.com/topic/172256-solved-find-time-between-curent-time-and-a-timestamp-in-a-db/#findComment-908252 Share on other sites More sharing options...
ignace Posted August 28, 2009 Share Posted August 28, 2009 $diff = time() - strtotime($row['time']); Quote Link to comment https://forums.phpfreaks.com/topic/172256-solved-find-time-between-curent-time-and-a-timestamp-in-a-db/#findComment-908284 Share on other sites More sharing options...
lynxus Posted August 28, 2009 Author Share Posted August 28, 2009 hheh, thanks, Getting closer. Seems to still be quite far out. while($row = mysql_fetch_array($result)) { $diff = time() - strtotime($row['time']); echo $row['time']; echo "<br>"; echo time(); echo "<br>"; echo strtotime($row['time']); $lasttime = strftime('%ed %Hh %Mm %Ss ago', $diff); } Result: 2009-08-28 13:22:44 1251462463 1251462164 Showing : 1d 01h 05m 44s ago What could be wrong here? Last hit shows to be around 10minutes ago. Thanks again Graham Quote Link to comment https://forums.phpfreaks.com/topic/172256-solved-find-time-between-curent-time-and-a-timestamp-in-a-db/#findComment-908288 Share on other sites More sharing options...
ignace Posted August 28, 2009 Share Posted August 28, 2009 I already guessed that these wouldn't work. Then the normal way I guess: $lasttime = strtotime($row['time']); $diff = time() - $lasttime; $days = floor($diff / 86400); $hours = floor(($diff % 86400) / 3600); $minutes = floor((($diff % 86400) % 3600) / 60)); $seconds = floor(((($diff % 86400) % 3600) % 60))); Quote Link to comment https://forums.phpfreaks.com/topic/172256-solved-find-time-between-curent-time-and-a-timestamp-in-a-db/#findComment-908291 Share on other sites More sharing options...
lynxus Posted August 28, 2009 Author Share Posted August 28, 2009 SWEEEEET works great Thankyou so much. I bow down to your php knowledge Quote Link to comment https://forums.phpfreaks.com/topic/172256-solved-find-time-between-curent-time-and-a-timestamp-in-a-db/#findComment-908296 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.