Jump to content

[SOLVED] How many users online in the last 24-hours using time()?


Kryptix

Recommended Posts

I'm fairly new to PHP. I have a MySQL table called 'users' and a field called 'loggedin' which is in time() format.

 

I need a query that'll check how many players have logged in within the last 24-hours, but I'm not sure where to start. I already have the database connection setup, and just need the amount to be set to "$online24h"

 

Can anyone help me please? :D

Link to comment
Share on other sites

$result = $db->query('SELECT COUNT(user) FROM '.$db->prefix.'rscd_players WHERE `loggedin` BETWEEN NOW() - '24:00:00' AND NOW()) or error('Unable to fetch total player count', __FILE__, __LINE__, $db->error());
$stats['online24'] = $db->result($result);

 

Parse error: syntax error, unexpected T_LNUMBER in C:\xampp\htdocs\index.php on line 127

 

Any idea?

 

Also, could I have the query for a week and a month too please?

Link to comment
Share on other sites

You use single quotes around the strin, so you can't use them IN the string.

$db->query('SELECT COUNT(user) FROM '.$db->prefix.'rscd_players WHERE `loggedin` BETWEEN NOW() - "24:00:00" AND NOW())

 

for a week it would look like this

 

SELECT
COUNT(*)
FROM players
WHERE loggedin BETWEEN DATE_ADD(NOW(), INTERVAL 7 DAY) AND NOW()

 

you should be able to figure the query for a month by yourself ;)

Link to comment
Share on other sites

Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\index.php on line 127

 

$result = $db->query('SELECT user, username FROM '.$db->prefix.'rscd_players ORDER BY creation_date DESC LIMIT 1') or error('Unable to fetch newest registered player', __FILE__, __LINE__, $db->error());
$stats['last_player'] = $db->fetch_assoc($result);

Link to comment
Share on other sites

Thanks. No errors now. However, it's showing ALL 5,000+ users as being online in the last 24-hours. Some have their 'login_date' set as '0' so something is wrong.

 

$db->query('SELECT COUNT(user) FROM '.$db->prefix.'rscd_players WHERE login_date BETWEEN NOW() - "24:00:00" AND NOW()');
$stats['online24'] = $db->result($result);

Link to comment
Share on other sites

'login_date' is a int(10) using PHP's time() function, I believe.

 

An example is: '1256418479'

 

Any suggestions? I really need this to work. :'(

 

So it's in fact unix timestamp... Ok then:

 

SELECT
COUNT(*)
FROM players
WHERE FROM_UNIXTIME(loggedin) BETWEEN DATE_ADD(NOW(), INTERVAL 1 DAY) AND NOW()

 

Link to comment
Share on other sites

I changed that. It's still displaying them all. :(

 

It's entered into 'login_date' which is a int(10). It updates 'login_date' with time() when they manage to login.

 

The exact code I have is:

 

$db->query('SELECT COUNT(*) FROM_UNIXTIME(login_date) BETWEEN DATE_ADD(NOW(), INTERVAL -1 DAY) AND NOW()');
$stats['online24'] = $db->result($result);

 

<dd><?php echo 'Game Characters Online Today: <strong>'. number_format($stats['online24']) ?></strong></dd>

 

:shrug:

Link to comment
Share on other sites

Doh!

We've lost WHERE somewhere along the line :P Thias query should not work at all, so make sure you're actually uploading your changes to the server ;)

 

This should do better

$db->query('SELECT COUNT(*) FROM '.$db->prefix.'rscd_players WHERE FROM_UNIXTIME(login_date) BETWEEN DATE_ADD(NOW(), INTERVAL -1 DAY) AND NOW()');

Link to comment
Share on other sites

Oops! I forgot to declare $result. :'(

 

Here's the code now:

 

$result = $db->query('SELECT COUNT(*) FROM_UNIXTIME(login_date) BETWEEN DATE_ADD(NOW(), INTERVAL -1 DAY) AND NOW()');
$stats['online24'] = $db->result($result);

 

This returns '0' (after being number_format()'d).

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.