显示用户最后一次在线使用PHP的显示



我需要帮助尝试在最后时间展示用户。我的目的是这样做,以便如果用户状态是"在线",则刚才显示的最后显示,否则将显示他们上次看到的。或者,如果更好的话,只需显示他们的最后一个在线时间,包括秒。

我的代码有效,但由于某种原因,当用户离线时,如果我在不到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>';
        }
    }
}

尝试此

   $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';
                }

相关内容

  • 没有找到相关文章

最新更新