Jump to content

Online now script (Compare timestamps)


El Chupacodra

Recommended Posts

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>

 

 

Link to comment
https://forums.phpfreaks.com/topic/254510-online-now-script-compare-timestamps/
Share on other sites

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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.