phpfan101 Posted December 14, 2009 Share Posted December 14, 2009 So, on my website, i have added a "last active" feature, so if someone is surfing the site, they can view when another users last page visit was. so, every second, +1 is added to the $ts variable what i have now, working and all, shows ONLY "NOW!", or ONLY "1 second", ONLY "___ seconds ago"... and so on I have tried over, and over, and over to fix this to my liking, with more instances available, as an example, my current script will show somebody who is 13 minutes and 26 seconds inactive, just as "13 Minutes", i want to see the "26 seconds" too... some thing "Last Active: 13 Minutes and 26 seconds ago" function howlongtil($ts) { $ts=$ts - time(); if ($ts<1) // <1 second return " NOW"; elseif ($ts==1) // <1 second return $ts." second"; elseif ($ts<60) // <1 minute return $ts." seconds"; elseif ($ts<120) // 1 minute return "1 minute"; else // <1 hour return floor($ts/60)." minutes"; }; So This is what i've got, anyone got a clue as to how i can amend it to show all the differences? Quote Link to comment Share on other sites More sharing options...
killerb Posted December 15, 2009 Share Posted December 15, 2009 I used this function to display time remaining until an online auction finishes, which is typically days, hours, mins and secs. It does not handle leap years or months, but it may be sufficient for what you need: <?php $from_tstamp = mktime(0, 0, 0, 2, 1, 2009); $to_tstamp = mktime(0, 0, 2, 2, 1, 2009); var_dump(timestamp_difference_units_to_string(timestamp_difference_to_units($from_tstamp, $to_tstamp))); function timestamp_difference_to_units($from_tstamp, $to_tstamp){ $from_tstamp = intval($from_tstamp); $to_tstamp = intval($to_tstamp); $seconds_remaining = $to_tstamp-$from_tstamp; if($seconds_remaining < 0){ $retval = timestamp_difference_to_units($to_tstamp, $from_tstamp); foreach($retval as $k=>$v){ $retval[$k] = -$v; } return $retval; } $num_years = 0; $num_days = 0; $num_hours = 0; $num_mins = 0; $num_secs = 0; while($seconds_remaining >= 31536000){ $num_years++; $seconds_remaining -= 31536000; } while($seconds_remaining >= 86400){ $num_days++; $seconds_remaining -= 86400; } while($seconds_remaining >= 3600){ $num_hours++; $seconds_remaining -= 3600; } while($seconds_remaining >= 60){ $num_mins++; $seconds_remaining -= 60; } $num_secs = $seconds_remaining; $seconds_remaining = 0; return array( 'years' => $num_years, 'days' => $num_days, 'hours' => $num_hours, 'minutes' => $num_mins, 'seconds' => $num_secs ); } function timestamp_difference_units_to_string($diff){ $parts = array(); if($diff['years']>0){ $parts[] = $diff['years'].' years'; } if($diff['days']>0){ $parts[] = $diff['days'].' days'; } if($diff['hours']>0){ $parts[] = $diff['hours'].' hours'; } if($diff['minutes']>0){ $parts[] = $diff['minutes'].' mins'; } if($diff['seconds']>0){ $parts[] = $diff['seconds'].' secs'; } $last_part = array_pop($parts); $retval = implode(', ', $parts); if(count($parts)>0){ $retval .= ' and '.$last_part; }else{ $retval .= $last_part; } return $retval; } Quote Link to comment Share on other sites More sharing options...
shane18 Posted December 15, 2009 Share Posted December 15, 2009 Shouldn't $ts=$ts - time(); be $ts=time() - $ts; Quote Link to comment Share on other sites More sharing options...
phpfan101 Posted December 15, 2009 Author Share Posted December 15, 2009 Ah! killerb! thanks! I see what you have now, and i think ill just completely rewrite mine, tyvm 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.