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 Link to comment https://forums.phpfreaks.com/topic/60247-solved-okay-another/ 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. Link to comment https://forums.phpfreaks.com/topic/60247-solved-okay-another/#findComment-299693 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. Link to comment https://forums.phpfreaks.com/topic/60247-solved-okay-another/#findComment-299696 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? Link to comment https://forums.phpfreaks.com/topic/60247-solved-okay-another/#findComment-299740 Share on other sites More sharing options...
MrXander Posted July 16, 2007 Author Share Posted July 16, 2007 Any ideas people? Link to comment https://forums.phpfreaks.com/topic/60247-solved-okay-another/#findComment-299755 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. Link to comment https://forums.phpfreaks.com/topic/60247-solved-okay-another/#findComment-299897 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.