Jump to content

[SOLVED] Okay, another...


MrXander

Recommended Posts

<?php
$lastActive = time() - $lastTime
if($lastActive >= 60){
//More than 60 seconds ago
$lastActive = intval($lastActive/60);
$timeType = "Minutes";
} else {
$timeType = "Seconds";
}

echo 'User was active '.$lastActive.' '.$timeType.' ago.';

?>

 

That's the first thing that came to my head. Didn't check the code, but it probably works.

Link to comment
https://forums.phpfreaks.com/topic/60247-solved-okay-another/#findComment-299693
Share on other sites

First: Subtract the last active timestamp from time().

 

Then, check it against various values:

 

Less than 60 = seconds

Less than 3600 = minutes

Less than 86400 = hours

Less than 604800 = days

Etc.

 

$active = time() - $last_active;
if($last_active < 60)
{
    $last_active_string = $last_active . ($last_active == 1) ' second' : ' seconds';
}
elseif($last_active < 3600)
{
    $last_active = (int) ($last_active / 60);
    $last_active_string = $last_active . ($last_active == 1) ' minute' : ' minutes';
}
elseif($last_active < 86400)
{
    $last_active = (int) ($last_active / 3600);
    $last_active_string = $last_active . ($last_active == 1) ' hour' : ' hours';
}
elseif($last_active < 604800)
{
    $last_active = (int) ($last_active / 86400);
    $last_active_string = $last_active . ($last_active == 1) ' day' : ' days';
}
...

 

That's pretty basic, but should give you a good idea.

Link to comment
https://forums.phpfreaks.com/topic/60247-solved-okay-another/#findComment-299696
Share on other sites

<?php
$lastActive = time() - $lastTime
if($lastActive >= 60){
//More than 60 seconds ago
$lastActive = intval($lastActive/60);
$timeType = "Minutes";
} else {
$timeType = "Seconds";
}

echo 'User was active '.$lastActive.' '.$timeType.' ago.';

?>

 

That's the first thing that came to my head. Didn't check the code, but it probably works.

 

That worked fantastically. How can I modify it to echo "hours" and "days", if it were to go that far?

Link to comment
https://forums.phpfreaks.com/topic/60247-solved-okay-another/#findComment-299740
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.