Free PHP Class: Display human friendly relative dates and times

One of the really lovely features that social sites like Twitter and Facebook give to users is a nice friendly way of displaying relative dates and times. For example:

"1 hour ago"
"Next Tuesday"
"Next month"

This PHP class converts an SQL date into a human friendly string representing that date and time relative to now.

You can try it out here:

Just enter an SQL formatted date or date/time string below:


 

 

The code

The code for using this class is really simple. Just include the class, instantiate the object, and pass it an SQL date:

require_once('humanRelativeDate.class.php');
$humanRelativeDate = new HumanRelativeDate();
$string = $humanRelativeDate->getTextForSQLDate($sql_date);

What could be easier?

Download

You can download humanRelativeDate.class.php here

Daylight saving

Please note that this script uses 86400 seconds as its basis for events less than 2 days away, and therefore does not correctly account for daylight saving days, where the day can be either 82800 in case its the winter change or 90000 for the summer change.

As a result, when viewing this script on a DST day, some unexpected results may occur: for example, an event which occurs on midnight the day after may be labelled as occuring "in two days", rather than "tomorrow"

We may address this in a future version.

Legalese

Whilst we've made every effort to make this script both bomb and idiot proof, the usual disclaimers apply: Invent Partners accepts no liability for any loss or damages incurred by the use or misuse of this script. Sorry.

 

“ Invent Partners supported my cycling event with a stylish website, intuitive CMS and flexible and responsive ecommerce system. Thanks to their hard work and patience, my event sold out in hours, with riders praising the easy to use event entry system and customer service. Many thanks! ”
Danial, London Edinburgh London

Contact Us