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.

 

“ Just wanted to send a quick 'thank you' to the Invent Partners team for all your help and support. I love the design of our new site and really appreciate the great service you provided at every stage - you turned a daunting project into one that I really enjoyed! Thanks again. ”
Lou, Advanced Writing Solutions

Contact Us

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