Help with last online on staff page

AMG

Well-Known Member
Dec 7, 2014
200
73
So I'm trying to figure out how to add for example '(number) hours ago' shown down below on the bottom of my motto on the staff page
need help asap

You must be registered for see images attach
 

Queso

echo 'Web Developer';
Nov 29, 2016
233
72
@AMG
Code:
<?php if($Users['online'] == 1){ $online = " Right now "; } else { $online = " ". date("H", $Users['last_online']) ." "; } ?>
Then just echo the output for the code above
Code:
echo "{$online} hours ago";
 

Haid

Member
Dec 20, 2011
363
448
@AMG
Code:
<?php if($Users['online'] == 1){ $online = " Right now "; } else { $online = " ". date("H", $Users['last_online']) ." "; } ?>
Then just echo the output for the code above
Code:
echo "{$online} hours ago";
That will show the hour that they were last online, for example if they were last online last night at 11pm it will still show 23 hours ago.
You need to use a function to get the relative time in relation to the timestamp you give it, when I did our HK I just found one online as it's standalone from Laravel which uses Carbon. You could use Carbon if you needed more options but this small function does what you want for a simple last online, you could even remove the second part which is for timestamps in the future as that will never occur in this case.

Code:
function time2str($ts) {
    if(!ctype_digit($ts)) {
        $ts = strtotime($ts);
    }
    $diff = time() - $ts;
    if($diff == 0) {
        return 'now';
    } elseif($diff > 0) {
        $day_diff = floor($diff / 86400);
        if($day_diff == 0) {
            if($diff < 60) return 'just now';
            if($diff < 120) return '1 minute ago';
            if($diff < 3600) return floor($diff / 60) . ' minutes ago';
            if($diff < 7200) return '1 hour ago';
            if($diff < 86400) return floor($diff / 3600) . ' hours ago';
        }
        if($day_diff == 1) { return 'Yesterday'; }
        if($day_diff < 7) { return $day_diff . ' days ago'; }
        if($day_diff < 31) { return ceil($day_diff / 7) . ' weeks ago'; }
        if($day_diff < 60) { return 'last month'; }
        return date('F Y', $ts);
    } else {
        $diff = abs($diff);
        $day_diff = floor($diff / 86400);
        if($day_diff == 0) {
            if($diff < 120) { return 'in a minute'; }
            if($diff < 3600) { return 'in ' . floor($diff / 60) . ' minutes'; }
            if($diff < 7200) { return 'in an hour'; }
            if($diff < 86400) { return 'in ' . floor($diff / 3600) . ' hours'; }
        }
        if($day_diff == 1) { return 'Tomorrow'; }
        if($day_diff < 4) { return date('l', $ts); }
        if($day_diff < 7 + (7 - date('w'))) { return 'next week'; }
        if(ceil($day_diff / 7) < 4) { return 'in ' . ceil($day_diff / 7) . ' weeks'; }
        if(date('n', $ts) == date('n') + 1) { return 'next month'; }
        return date('F Y', $ts);
    }
}

You can then just give it your timestamp.
Code:
 time2str($row['last_online'])

9fee04d01.png
 

Users who are viewing this thread

Top