1

I have created a simple online users script in php, so far it stands as this.

$online_users = time() - 900;

$q = $dbc -> prepare("SELECT * FROM accounts WHERE online > ? ORDER BY username");
$q -> execute(array($online_users));
$result = $q -> rowCount();

On every member's page the row 'online' gets updated with the current time() with every user click.

As you can see $online_users, is the current time minus 15 minutes. I can then find out if they have been active on the site in the last 15 minutes and do a while loop to dislay them all.

My question is, how can I find out the exact time in seconds that they haven't been active for so I can display a message saying;

'Last active 45seconds ago' or 'Last active 14mins ago'

I hope I have explained this well enough and thanks :)

2 Answers 2

1

I think I might have misread your question, but this should be as simple as subtracting the last activity time (in your DB) from the current time.

$inactive_time = time() - last_activity;

You could then determine whether to show "seconds" or "minutes" by seeing if "$inactive_time" is greater than "60".

1

Try this one:

$resulted_account = $q->fetchAll();

foreach($resulted_account as $account) {
    $inactive_time = $account['online'] - $online_users;
    if ($inactive_time < 60)  {
        echo "Last active $inactive_time seconds ago";
    } else {
        echo "Last active" .date("i", $inactive_time) . "minutes ago";        
    }
}
2
  • this works bug with some bugs in it, but it is definetely better than what I had thanks
    – carlgcoder
    Aug 27, 2011 at 21:06
  • @carlgcoder I can't check this code so this only example of how you can do it.
    – Andrej
    Aug 27, 2011 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.