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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.