For a side project that I was working on recently, we wanted to display dates for content using the “time ago” format as opposed to showing a literal date/time, for instance “Posted 10 minutes ago” or “Posted 2 days ago”, etc.
This is quite simple to implement server side with a function such as this that takes a date and returns a nicely worded string in the form of “X mins/hours/days/weeks/months ago”. However for this project we needed to do heavy page caching, rendering the page in PHP first and then caching the full HTML for 30 minutes, long enough that the time ago format would quickly show outdated numbers.
To the rescue……a very nice Jquery plugin called appropriately enough, timeago. Since time ago is calculated and rendered client side with this plugin, you can cache pages as long as you want and yet still have accurate time ago numbers displayed. As a bonus, the numbers even auto-update as time passes without the need for a page refresh, making them even more accurate than a pure server side solution.
And to cover all bases, you can still do a server side time ago calculation, output that string, and then let the timeago Jquery plugin update that number in real time client side.