Take the 2-minute tour ×

Possible Duplicate:
How do I calculate relative time?

I want to format dates on my social web app much like Digg.com and other sites do. There, you see very friendly dates, such as:

  • just now
  • 3 minutes ago
  • one hour ago
  • 2 weeks ago
  • 6 monts ago

...you get the idea. Before I wrap my head around creating such a thing, does anyone know of any ready-to-go script for this where I simply insert a datestamp and a friendly date text is given based on how it related to the current time?

PS: I need this in PHP, but pseudo-code or any other language is fine too.

share|improve this question

marked as duplicate by Shog9 Sep 15 '11 at 21:57


add comment

3 Answers

up vote 8 down vote accepted

This is a duplicate of this question. It has a flurry of code samples on how to accomplish this, in addition to the code this very site uses. I glanced at it and there seems to be a PHP implementation posted there too.

In addition to all this, if are you using jQuery you can do this client-side with something like the timeago plugin. It has the advantage of updating the text as time passes so if you load a page and it says "posted 5 minutes ago" and look again 5 minutes later, it says "posted 10 minutes ago"

share|improve this answer
add comment

Thanks all for the answers, and sorry for the duplicate question. I did not find the duplicate when I was looking for it because I did not really know what search terms to use.

Anyways, I have my problem solved thanks to the PHP translation of the code used by stackoverflow. I made one tiny change in calculating the delta:

$delta = strtotime(gmdate("Y-m-d H:i:s", time())) - $time;

Since I am storing my dates in MySQL as timestamp in the GMT format, I have to use the same for calculating the CURRENT time. This makes for a timezone neutral comparison, which is exactly what is needed in my case.

share|improve this answer
add comment
