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.

 

“ From the outset, invent Partners got the balance right and were prepared to listen to our needs, whilst at the same time offering their depth of knowledge and experience. The end product we received is exceptional and has since proven its worth, fundamentally from a ‘back end’ input and management information viewpoint and importantly from a ‘front end’ consumer ease of use stance. Needless to say, we are growing our business as a result, with 80% of web business as ‘net new’. ”
Ged, Vertigrow Ltd

Contact Us

© 2024 Invent Partners Ltd | Supporters of FdA Web Design, Wakefield