Jump to content

Last Seen (Days, Hours, Minutes)


-Karl-

Recommended Posts

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

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';

Edited by Jessica
Link to comment
Share on other sites

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

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