• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

Give Your Visitors a Relative Time

By Akash Mehta | on Mar 7, 2008 | 3 Comments
PHP Tutorials
Gmail timestamps
  • Tweet
  • Share
  • Tweet
  • Share

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.

Share this story:
  • tweet

Tags: php tipsPHP Tutorials

Author Description

3 Responses to “Give Your Visitors a Relative Time”

  1. December 3, 2008

    Kyle Log in to Reply

    Just want you to know that the line:

    if ($diff == 1) $term .= “s”;

    should read

    if ($diff != 1) $term .= “s”;

  2. February 15, 2009

    Imad Log in to Reply

    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.

  3. May 6, 2009

    Gruppler Log in to Reply

    Actually, Imad, Kyle is correct. Unless you say, “Negative twenty hour.”

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 1,240Followers 494Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    May 2013
    M T W T F S S
    « Oct    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.