Ben Za
Ben Za

Reputation: 31

Display when user was last online using 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>';
        }
    }
}

Upvotes: 3

Views: 2408

Answers (1)

Sunday Etom
Sunday Etom

Reputation: 341

try this

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

Upvotes: 1

Related Questions