Posted by Chris B on 05/10/05 22:14
> $last_activity = '2005-04-28 18:38:49';
> $time_since_last_activity = time() - strtotime($last_activity);
> $days = (int) floor($time_since_last_activity / (24*60*60));
> $hrs = (int) floor($time_since_last_activity %
> ($days*24*60*60) / (60*60));
> $mins = (int) floor($time_since_last_activity %
> ($days*24*60*60 + $hrs*60*60) / 60);
> $secs = (int) floor($time_since_last_activity %
> ($days*24*60*60 + $hrs*60*60 + $mins*60));
> echo "Last activity: $days days $hrs hrs $mins mins $secs secs ago";
That works great but only if the $time_since_last_activity > 86400.
i.e. if the difference is less than a day, $days ends up as 0.something,
which floors to zero.
$hrs then = difference % (0*24*60*60 / 60*60) which is (0/3600).
I think it would be the same if the difference was < 1 hour and 1 min
and 1 sec respectively.
I tried running various if difference > 86400 {work out days] etc but
I've just got myself confused.
Is there a way to work the values if it is under < 1 day, < 1 hour etc.
Navigation:
[Reply to this message]
|