Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 
Webmaster Blog

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:

Gmail timestamps

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.

Tags: ,


Related Posts
» Calculating date difference more precisely in PHP
» Choosing the Best Colors for your Website
» Blog Stats Series: What to do when your pageviews are sinking like the Titanic
» How to Measure SEO Success
» Paul Reinheimer’s PHP Contest
 



2 Responses to “Give Your Visitors a Relative Time”

  1. Calculating date difference more precisely in PHP Says:

    […] Following this post by Akash Mehta, I thought I that a precise datDiff() function might be helpful for many of you. The following function calculates the difference in “day” or “month” or “year” more precisely. This function is production ready and you can use it in any of you application which mainly works with these date difference. I have found somewhere in web, just forgot the source. Thanks to the unknown author of this excellent function. […]

  2. The Back Burner » Downright Slippery Time Says:

    […] here’s a function for calculating differences betwen times. Untested for resistance to DST, but could be useful in some situations. Ironically, the post he […]

Leave a Reply

Advertise with Us!


Blog Categories Blog Archives


Tutorials Scripts Web Hosting Developer Manuals
Resources