douhu2898 2017-02-22 16:17
浏览 66

使用PHP在用户上次上线时显示

I'm needing help with trying to display the users last online time. My aim is to make it so that if the users status is 'online', display last seen just now, else, display the last time they were seen. Or if it's better, just display their last online time including seconds.

My code works but for some reason, when the user is offline, it displays user last online 1 hour ago if it I logged out less than 10 seconds ago.

My code is below

//Our default online script to get the last active time period from our lastactive time
function time_since($since)
{
    global $con;
    global $user_infos;

    foreach($user_infos as $user)
    {
        $last_active  = $user[7];
        $user_status  = $user[6];

        $chunks = array(
            array(60 * 60 * 24 * 365 , 'year'),
            array(60 * 60 * 24 * 30 , 'month'),
            array(60 * 60 * 24 * 7, 'week'),
            array(60 * 60 * 24 , 'day'),
            array(60 * 60 , 'hour'),
            array(60 , 'minute'),
            array(1 , 'second')
        );

        for ($i = 0, $j = count($chunks); $i < $j; $i++)
        {
            $seconds    = $chunks[$i][0];
            $name       = $chunks[$i][1];
            if (($count = floor($since / $seconds)) != 0)
            {
                break;
            }
        }

        $print = ($count == 1) ? '1 '.$name : "$count {$name}s";
        $check = $user_status == 'online' ? 'last online just now' : 'last online '.$print;
        return $check;
    }
}


// I made to make it firstly, update the users lastactive time for each page they go on by calling the function user_online_check() in the header (which is called for every page)
// and secondly, make it return a green image if the client is online and a red image if the client is offline
function user_online_check()
{
    global $con;
    global $user_infos;
    global $time;


    foreach($user_infos as $user)
    {
        $username = $_SESSION["user_i"];
        $now      = date("Y-m-d H:i:s");

        //mysqli_query($con, "UPDATE users SET lastactive = NOW() WHERE username = '$username'");

        $last_active  = $user[7];
        $user_status  = $user[6];

        mysqli_query($con, "UPDATE users SET status = 'offline' WHERE TIMESTAMPDIFF(MINUTE, lastactive, NOW()) > 1 AND username = '$username' LIMIT 1");
        mysqli_query($con, "UPDATE users SET status = 'online' WHERE TIMESTAMPDIFF(MINUTE, lastactive, NOW()) < 1 AND username = '$username' LIMIT 1");


        if($user_status == 'online')
        {
            echo '<center><img src="images/user/bullet_green.ico" style="width: 20px; height: 20px;" title="'.time_since(time() - strtotime($last_active)).'"></center>';
        }
        else if($user_status == 'offline')
        {
            echo '<center><img src="images/user/bullet_red.ico" style="width: 20px; height: 20px;" title="'.time_since(time() - strtotime($last_active)).'"></center>';
        }
    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算