开发者

Subtracting time() from mysql table for last activity?

开发者 https://www.devze.com 2023-04-01 09:35 出处:网络
I have cr开发者_StackOverflow社区eated a simple online users script in php, so far it stands as this.

I have cr开发者_StackOverflow社区eated 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 :)


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".


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";        
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消