Give Your Visitors a Relative Time
Toby Somerville recently blogged about giving visitors a rough time – instead of displaying exact timestamps, providing a rough written approximation of the time.
Toby suggested that we generally communicate in approximate times, but on the web we usually work with exact timestamps. His idea was that by displaying the time as you would describe it verbally, he could better communicate the time in a visitor-friendly way.
After reading his post, I understood the merits of the approach, but it struck me that a relative time might be a little more useful for many situations. For example, in a fast moving discussion, a short timestamp (e.g. 8:30 AM) as well as a verbal summary of how long ago the time was (e.g. “4 hours ago”, “20 minutes ago”) are most useful to the user. In fact, this is precisely what Gmail does:

A common feature of Gmail’s timestamp system is approximation, similar to what Toby was getting at. For example, as you approach 60 minutes in the hour, Gmail seems to switch from “x minutes ago” to “1 hour ago”, even if you’re not quite there.
Now, I figured this could be achieved very easily in PHP – and it could. After the jump, here’s the code:
<?php
function RelativeTime($time, $now = false)
{
$time = (int) $time;
$curr = $now ? $now : time();
$shift = $curr - $time;
if ($shift < 45):
$diff = $shift;
$term = "second";
elseif ($shift < 2700):
$diff = round($shift / 60);
$term = "minute";
elseif ($shift < 64800):
$diff = round($shift / 60 / 60);
$term = "hour";
else:
$diff = round($shift / 60 / 60 / 24);
$term = "day";
endif;
if ($diff == 1) $term .= "s";
return "$diff $term ago";
}
It's got a few unusual constants in there - the number values refer to 75% of the number of seconds in a particular period of time. 75% seemed like a reasonable measure beyond which we could round up - for example, the function will say "45 seconds ago" for 45 seconds, but "1 minute ago" for 46. The same goes for 2700 seconds, equal to 45 minutes, and 64800 seconds, or eighteen hours. Beyond this it simply uses the number of days.
The function will give you values like "20 seconds ago", "10 minutes ago", "3 hours ago" etc. Adding the Numbers_Words PEAR package might improve readability of your timestamps even more
So give it a go - it's especially handy for message boards, comment lists and news readers, especially with threaded discussion. The code is very basic at the moment, suitable for adapting to whatever purpose you see fit.
EDIT: Hasin Hayder, a fellow DT blogger, wrote a post about a similar function that you may be interested in.






December 3rd, 2008 at 5:22 pm
Just want you to know that the line:
if ($diff == 1) $term .= “s”;
should read
if ($diff != 1) $term .= “s”;
February 15th, 2009 at 10:13 pm
Actually Kyle,
this:
if ($diff != 1) $term .= “s”;
Should be this:
if ($diff > 1) $term .= “s”;
Since we’re checking if $diff is greater than 1 not that it doesn’t equal 1.
May 6th, 2009 at 12:02 am
Actually, Imad, Kyle is correct. Unless you say, “Negative twenty hour.”