Give Your Visitors a Rough Time

Whilst out surfing the big WWW I came across Laurence Willmott project page about how people really tell the time.

When asked the time, we generally don’t need to be military accurate with our response and so don’t tend to say ‘its nine fifty three and twenty two seconds’. We generally communicate the approximate time. i.e. ‘its nearly ten’ or ‘its just gone half past three’. Yet on the web, time is generally shown as ’12:24:13 AM’, or similar — not very visitor friendly.

This gave me an idea: what about doing a similar thing on a web site. i.e. showing the time as you would say it. So, without further ado I give you: “RoughTime” a bit of PHP code to express the time — roughly.


< ?php
$hour = (int) date('g');
$minute = (int) date('i');

switch($minute){
	case ($minute >=0 && $minute <5):
		$roughTime = numberToWord($hour).'-ish';
	break;
	case ($minute >=5 && $minute <14):
		$roughTime = 'just gone '.numberToWord($hour);
	break;
	case ($minute >=15 && $minute <20):
		$roughTime = 'quarter past '.numberToWord($hour);
	break;
	case ($minute >=20 && $minute <25):
		$roughTime = 'nearly half '.numberToWord($hour);
	break;
	case ($minute >=25 && $minute <35):
		$roughTime = 'half '.numberToWord($hour);
	break;
	case ($minute >=35 && $minute <40):
		$roughTime = 'just gone half '.numberToWord($hour);
	break;
	case ($minute >=40 && $minute <50):
		$roughTime = 'quarter to '.numberToWord($hour + 1);
	break;
	case ($minute >=50):
		$roughTime =  'nearly '.numberToWord($hour +1);
	break;
}

function numberToWord($number){
	switch($number){
		case 1:
			$word = 'one';break;
		case 2:
			$word = 'two';break;
		case 3:
			$word = 'three';break;
		case 4:
			$word = 'four';break;
		case 5:
			$word = 'five';break;
		case 6:
			$word = 'six';break;
		case 7:
			$word = 'seven';break;
		case 8:
			$word = 'eight';break;
		case 9:
			$word = 'nine';break;
		case 10:
			$word = 'ten';break;
		case 11:
			$word = 'eleven';break;
		case 13:
			$word = 'one';break;
		default:
			$word = 'twelve';break;
	}
	return $word;
}

echo 'Its '.$roughTime;
?>

The code is deliberately in a pretty raw state, so you can adapt it to suit.

How does it work?
First off, it gets the minute and hour of the current time as integers. Next, it selects the appropriate message based on the minutes past the hour and finally sets the sets the hour number as a word.

A useful alternative to the “traditional” way of displaying time? Go on give your site visitors a rough time, you never know — they might like it.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • Joshua Paine

    One cultural note: on the East coast of the States, here, I’ve never heard anyone say “just gone two”: we’d say it’s “just past two”.

    One style note: switch is lame. here’s numberToWord in two lines:

    
    function hourToWord($hour) {
      $hrs = array('twelve','one','two','three','four','five','six','seven',
        'eight','nine','ten','eleven');
      return $hrs[$hour%12];
    }
    

    Use it with $hour = (int) date('G'); instead of date('g').

    Bonus style note: a function named “numberToWord” should never do such things as return “one” when passed 13 or “twelve” when passed 9078. hourToWord is a much more honest name.

  • Paul Annesley

    I’m not much of a golfer, but you could save yourself a few lines like so… :)

    
    function numberToWord($number)
    {
    	$numbers = array(null, 'one', 'two', 'three', 'four', 'five', 'six',
    		'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'one');
    	return isset($numbers[$number]) ? $numbers[$number] : 'twelve';
    }
    

    Edit: beaten to it by Joshua

  • Joshua Paine

    I swear I’ve been able to edit comments on here before, and Paul obviously did, but I can’t seem to get back in to my message to fix the code formatting or correct my JavaScript-style array creation. (Obviously it should have been array(‘twelve’,’one’…).)

  • http://www.cemerson.co.uk Stormrider

    Nice idea. The division of the hour is strange though. It is ‘just gone x’ right up until 14 minutes past, but only at 15 minutes past do you get ‘quarter past x’. Then on the other side, is ‘quarter to x+1′ from 40 to 50 minutes past.

  • http://www.magain.com/ mattymcg

    @Joshua Paine: Paul was only able to edit his comment because he’s an admin. I updated yours — yes, we know, the blog comment system is less than optimal.

    @Toby: Nice one. Obviously not appropriate in every instance, but there are probably a few places here on SitePoint where it might be worth considering.

  • http://www.yourusn.com/ brent5392

    I like it, I really do =P
    May very well use it in the future.
    Maybe it might be a good idea just to include the time (hours and minutes) after it just for anyone who would like to know the exact time. Keeps everyone happy =)

  • jc

    I would argue it takes more effort to think through someone verbally saying “12:24:13 AM” than someone saying “nearly twelve thirty”, and it takes more effort to read and think through “nearly twelve thirty” than “12:24 PM”

  • captain grammar

    neat script.

    I have one improvement:

    change:
    echo ‘Its ‘.$roughTime;

    to:
    echo ‘It’s ‘.$roughTime;

  • Anonymously

    Nice idea but just how helpful is it?

    I get the idea that it’s just going to confuse, maybe even scare the hell out of your visitors. Maybe you have forgotten the reasons behind displaying the date and time in a set format?

    It’s because it is uniform, it can be read and understood by just about every living person on the planet, no? Now your suggesting this approach?

    The Dr.

  • Anonymous

    A rough time is often really handy, but while we’re all for approximations I find relative times are even more helpful – e.g. “three hours ago”, “twenty minutes ago”, “yesterday”. Gmail does this a lot, and it really helps when working with threaded lists of messages (e.g. emails, forum posts, even comments like these). I put together a little script that achieves the basics of this — see my post on relative times in PHP for more details.

  • franglix

    I think it’s a nice idea, gives a rather relaxed tone – perhaps ideal for a blog. I will give it a try… Thanks.