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

时间:2017-02-22 16:17:48

标签: php offline status

我在尝试显示用户上次在线时间时需要帮助。 我的目的是使其成为如果用户状态为“在线”,则显示最后一次显示,否则显示上次查看时间。或者如果它更好,只需显示他们的最后在线时间,包括秒。

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

1 个答案:

答案 0 :(得分:0)

尝试

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