tags:

views:

24

answers:

2

Hi there,

is there a library out there for PHP that formats dates in an informal way?

I would love to have outputs like yesterday, 2 minutes ago, just now, three weeks ago – just like in Facebook. Makes blog posts appear so much more human.

Thanks in advance for your help.

+1  A: 

I dunno about a library, but plenty of snippet functions:

http://urbanoalvarez.es/blog/2009/01/20/display-time-since-last-visit-in-php/

http://binarybonsai.com/code/timesince.txt

The keywords are time since php and should pull up tons of snippets / code for you.

Brad F Jacobs
+1  A: 

Not sure about php but there's a jQuery plugin that does just that jQuery Pretty Date

Makes it easier to dynamically update the output, ie when a user stays on page for some time, the 2 minutes ago would update to say 3 minutes ago and so on

Example straight from that website

prettyDate("2008-01-28T20:24:17Z") // => "2 hours ago"
prettyDate("2008-01-27T22:24:17Z") // => "Yesterday"
prettyDate("2008-01-26T22:24:17Z") // => "2 days ago"
prettyDate("2008-01-14T22:24:17Z") // => "2 weeks ago"
prettyDate("2007-12-15T22:24:17Z") // => undefined 
Serge