Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Sameer Borate's Blog:
Printing relative dates in php
Jun 22, 2010 @ 15:52:26

Sameer Borate has a quick, helpful tip for anyone working with dates in PHP and wanting something a bit more readable than just the standard month-day-year kind of format.

A couple of days back while writing some date code for a messaging service, I required to print the date of the messages in a relative format – 'today', 'yesterday', '3 weeks ago' etc. I wrote a small function for the same.

The technique uses a "DateToWords" function he's created that finds the difference between now and the date being searched for and evaluates it to see how many days and weeks it might be offset from that day. The code for the function is included.

tagged: relative tutorial date datetowords

Link:

Developer Tutorials Blog:
Give Your Visitors a Relative Time
Mar 07, 2008 @ 17:19:00

Following on the heels of this post to the SitePoint blog (about "roughtime"), Akash Mehta has posted his own version of providing users with something a little different - relative time.

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. [...] Now, I figured this could be achieved very easily in PHP - and it could.

His code is included showing a series of if/else comparisons that change the values to their hour/minute/second counterparts. He also recommends the PEAR Numbers_Words package to help with readability.

tagged: relative time pear numberswords since

Link:


Trending Topics: