显示用户上次使用PHP联机的时间

显示用户上次使用PHP联机的时间,php,offline,status,Php,Offline,Status,我需要帮助来显示上次联机时的用户。 我的目标是,如果用户状态为“在线”,则显示刚才看到的最后一次,否则,显示上次看到他们的时间。或者,如果更好的话,只需显示他们最后的在线时间,包括秒数 我的代码可以工作,但由于某些原因,当用户脱机时,如果我在10秒前注销,它会显示用户1小时前最后一次联机 我的代码在下面 //Our default online script to get the last active time period from our lastactive time function

我需要帮助来显示上次联机时的用户。 我的目标是,如果用户状态为“在线”,则显示刚才看到的最后一次,否则,显示上次看到他们的时间。或者,如果更好的话,只需显示他们最后的在线时间,包括秒数

我的代码可以工作,但由于某些原因,当用户脱机时,如果我在10秒前注销,它会显示用户1小时前最后一次联机

我的代码在下面

//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>';
        }
    }
}
//从上次活动时间获取上次活动时间段的默认联机脚本
函数时间\u自($自)
{
全球$con;
全球$user\u infos;
foreach($user\u infos as$user)
{
$last_active=$user[7];
$user_status=$user[6];
$chunks=数组(
阵列(60*60*24*365,'年'),
数组(60*60*24*30,'月'),
数组(60*60*24*7,'周'),
数组(60*60*24,'天'),
数组(60*60,'小时'),
数组(60,'分钟'),
数组(1,'秒')
);
对于($i=0,$j=count($chunks);$i<$j;$i++)
{
$seconds=$chunks[$i][0];
$name=$chunks[$i][1];
如果(($count=floor($since/$seconds))!=0)
{
打破
}
}
$print=($count==1)?'1'。$name:“$count{$name}s”;
$check=$user_status=='online'?'last online now':'last online'。$print;
退回美元支票;
}
}
//首先,通过调用页眉中的函数user_online_check()来更新用户在每个页面上的最后活动时间(每个页面都会调用该函数)
//其次,如果客户端处于联机状态,则返回绿色图像;如果客户端处于脱机状态,则返回红色图像
函数用户\u联机\u检查()
{
全球$con;
全球$user\u infos;
全球美元时间;
foreach($user\u infos as$user)
{
$username=$\u会话[“用户i”];
$now=日期(“Y-m-d H:i:s”);
//mysqli_查询($con,“更新用户集lastactive=NOW(),其中用户名=“$username”);
$last_active=$user[7];
$user_status=$user[6];
mysqli_query($con,“更新用户设置状态='offline',其中TIMESTAMPDIFF(MINUTE,lastactive,NOW())>1,username='username'LIMIT 1');
mysqli_query($con,“更新用户设置状态='online',其中TIMESTAMPDIFF(MINUTE,lastactive,NOW())<1,username='username'LIMIT 1');
如果($user\u status=='online')
{
回声';
}
else if($user_status=='offline')
{
回声';
}
}
}
试试这个

   $seen = floor((time("now")-$row['time'])/60);
                $more = false;
                if($seen > 60) {
                    $more = true;
                    $hours = floor($seen/60);
                    $minutes = $seen-($hours*60);
                    if(($seen > 24) && ($more == true)) {
                        $days = floor(($seen/60)/24);
                        $hours = floor($seen/60)-($days*24);
                    }
                    if($minutes == 1) {
                        $minute = ' minute ';  
                    } else {
                        $minute = ' minutes ';
                    }
                    if($hours == 1) {
                        $hour = ' hour ';  
                    } else {
                        $hour = ' hours ';
                    }
                    if($days == 1) {
                        $day = ' day ';  
                    } else {
                        $day = ' days ';
                    }
                    if($days > 0) {  
                        $seen = $days . $day . $hours . $hour . $minutes . $minute . 'ago';
                    } else {
                        $seen = $hours . $hour . $minutes . $minute . 'ago';
                    }
                } else {
                    if($seen == 1) {
                        $minute = ' minute ';  
                    } else {
                        $minute = ' minutes ';
                    }    
                    $seen = $seen . $minute . 'ago';
                }

请包括一个如何以及为什么解决这个问题将真正有助于提高你的职位质量,并可能导致更多的投票的解释