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
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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.