-Karl- Posted September 29, 2012 Share Posted September 29, 2012 Hello, I'm trying to create a staff online widget which shows when they were last active. Here's what I have so far, it works perfectly fine but I'm just wondering if there's a much simpler way of doing it, if so, how? $seen = floor((time("now")-$row['time'])/60); $more = false; if($seen > 60) { $more = true; $hours = floor($seen/60); $minutes = $seen-($hours*60); if(($seen > 24) && ($more == true)) { $days = floor(($seen/60)/24); $hours = floor($seen/60)-($days*24); } if($minutes == 1) { $minute = ' minute '; } else { $minute = ' minutes '; } if($hours == 1) { $hour = ' hour '; } else { $hour = ' hours '; } if($days == 1) { $day = ' day '; } else { $day = ' days '; } if($days > 0) { $seen = $days . $day . $hours . $hour . $minutes . $minute . 'ago'; } else { $seen = $hours . $hour . $minutes . $minute . 'ago'; } } else { if($seen == 1) { $minute = ' minute '; } else { $minute = ' minutes '; } $seen = $seen . $minute . 'ago'; } Link to comment https://forums.phpfreaks.com/topic/268919-last-seen-days-hours-minutes/ Share on other sites More sharing options...
Jessica Posted September 29, 2012 Share Posted September 29, 2012 This will produce errors with strict error reporting. You can shorten it a lot. I also think it has some bugs. if $seen > 60, then within that loop if $seen > 24?? Maybe use clearer variable names. What is the $more for? This is not tested. $seen = floor((time("now")-$row['time'])/60); $more = false; $seen_str = ""; if($seen > 60) { $more = true; $hours = floor($seen/60); $minutes = $seen-($hours*60); if($seen > 24) { // I think you mean to use $hours here? $days = floor(($seen/60)/24); // and here $hours = floor($seen/60)-($days*24); // and probably in here too. $seen_str = "$days day".($days==1?'':'s').", "; } $seen_str .= "$hours hour".($hours==1:'':'s').", "; } $seen_str .= "$minutes minute".($minutes==1?'':'s').' ago'; Link to comment https://forums.phpfreaks.com/topic/268919-last-seen-days-hours-minutes/#findComment-1381797 Share on other sites More sharing options...
-Karl- Posted September 29, 2012 Author Share Posted September 29, 2012 This will produce errors with strict error reporting. You can shorten it a lot. I also think it has some bugs. if $seen > 60, then within that loop if $seen > 24?? Maybe use clearer variable names. What is the $more for? This is not tested. $seen = floor((time("now")-$row['time'])/60); $more = false; $seen_str = ""; if($seen > 60) { $more = true; $hours = floor($seen/60); $minutes = $seen-($hours*60); if($seen > 24) { // I think you mean to use $hours here? $days = floor(($seen/60)/24); // and here $hours = floor($seen/60)-($days*24); // and probably in here too. $seen_str = "$days day".($days==1?'':'s').", "; } $seen_str .= "$hours hour".($hours==1:'':'s').", "; } $seen_str .= "$minutes minute".($minutes==1?'':'s').' ago'; Yeah the $more is irrelevant. Like I said though, it works perfectly, so everything is in the right place. Removed a bit of pointless stuff after having a second look through the code if($seen > 60) { $hours = floor($seen/60); $minutes = $seen-($hours*60); $days = floor(($seen/60)/24); $hours = floor($seen/60)-($days*24); if($minutes == 1) { $minute = ' minute '; } else { $minute = ' minutes '; } if($hours == 1) { $hour = ' hour '; } else { $hour = ' hours '; } if($days == 1) { $day = ' day '; } else { $day = ' days '; } if($days > 0) { $seen = $days . $day . $hours . $hour . $minutes . $minute . 'ago'; } else { $seen = $hours . $hour . $minutes . $minute . 'ago'; } } else { if($seen == 1) { $minute = ' minute '; } else { $minute = ' minutes '; } $seen = $seen . $minute . 'ago'; } Basically, it's the defining whether it's an hour, or multiple hours, which makes the code look so long. Link to comment https://forums.phpfreaks.com/topic/268919-last-seen-days-hours-minutes/#findComment-1381799 Share on other sites More sharing options...
-Karl- Posted September 29, 2012 Author Share Posted September 29, 2012 After a bit of messing around with your advice, got it down to this: $hours = floor($seen/60); $minutes = $seen-($hours*60); $days = floor(($seen/60)/24); $hours = floor($seen/60)-($days*24); $last_seen = ''; if($days > 0) { $last_seen .= "$days day".($days==1?'':'s'); } if($hours > 0) { $last_seen .= " $hours hour".($hours==1?'':'s'); } $last_seen .= " $minutes minute".($minutes==1?'':'s'); Thanks a lot. Link to comment https://forums.phpfreaks.com/topic/268919-last-seen-days-hours-minutes/#findComment-1381801 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.