MrXander Posted July 16, 2007 Share Posted July 16, 2007 ..I want to show when a user was last active. Again, their latest action is recorded by time(); How can I show how many seconds ago that user was last active? And if more than 60 seconds, tick over into minutes... etc Quote Link to comment Share on other sites More sharing options...
lococobra Posted July 16, 2007 Share Posted July 16, 2007 <?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. Quote Link to comment Share on other sites More sharing options...
Oldiesmann Posted July 16, 2007 Share Posted July 16, 2007 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. Quote Link to comment Share on other sites More sharing options...
MrXander Posted July 16, 2007 Author Share Posted July 16, 2007 <?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? Quote Link to comment Share on other sites More sharing options...
MrXander Posted July 16, 2007 Author Share Posted July 16, 2007 Any ideas people? Quote Link to comment Share on other sites More sharing options...
uramagget Posted July 16, 2007 Share Posted July 16, 2007 Well, as for how an hour, you'd set the seconds to 3600, and for a day, it's 86400 seconds. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.