El Chupacodra Posted January 6, 2012 Share Posted January 6, 2012 Hi, I just want to see what way you guys think is best. On this little community I'm building I have decided to implement a function to see who were active within the last 15 minutes. I made a table (just user and timestamp) to register the last activity of any logged on user. Then I have a variable to take off 15 minutes from that but I can't get them to compare. Googling the issue I found people are solving this in very different ways. I wanted to see what phpfreaks recommend as the next step. Here is some code (that doesn't work properly - no results found as I compare to different timestamps): include_once'header.php'; $now=time(); $now=(date("Y-m-d H:i:s")); //$mins = mktime(0,$now-15,0, date("Y"), date("m"),date("d")); $mins = time(); $mins15 = $mins-(60*15); $mins15 = (date("Y-m-d H:i:s", $mins15)); $online="SELECT * FROM user_online" WHERE last_activity > mins15; $result = mysql_query($online); if (!$result) die ("Database access failed: " . mysql_error()); $rows = mysql_num_rows($result); echo <<< _END <div id='statusbar'> <h4>Online now: $rows</h4> Quote Link to comment https://forums.phpfreaks.com/topic/254510-online-now-script-compare-timestamps/ Share on other sites More sharing options...
Pikachu2000 Posted January 6, 2012 Share Posted January 6, 2012 If you're storing the times in a DATETIME field (YYYY-MM-DD format), you can forgo all the date/time calculations in php and do it with one simple query. SELECT field1, field2 FROM table WHERE last_activity > DATE_SUB(NOW(), INTERVAL 15 MINUTE) Quote Link to comment https://forums.phpfreaks.com/topic/254510-online-now-script-compare-timestamps/#findComment-1305019 Share on other sites More sharing options...
El Chupacodra Posted January 6, 2012 Author Share Posted January 6, 2012 This is why I love this forum. I never heard of that interval before - I will go try it out right away. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/254510-online-now-script-compare-timestamps/#findComment-1305026 Share on other sites More sharing options...
El Chupacodra Posted January 6, 2012 Author Share Posted January 6, 2012 Thank you! That worked like a charm! And you were lightning fast too. Quote Link to comment https://forums.phpfreaks.com/topic/254510-online-now-script-compare-timestamps/#findComment-1305032 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.