Relative Dates in ExpressionEngine 2.8

An additional date formatting option is coming to ExpressionEngine 2.8. You will be able to output any date as a relative amount of time using new, flexible, date tag parameters, a la:

{entry_date:relative stop="+1 day" format="%F %d, %Y"

Most of us use relative dates in everyday speech. We say things like “The last time I played ‘Railways of the World’ was about 4 years ago,” or “My shipment of board games will arrive in 2 days!” There is a famous speech that begins with, “Four score and seven years ago….” Being able to refer to an event in such terms is natural, but not always desired. Consider “July 4, 1776” versus “237 years ago” or “December 7, 1941”, that “date which will live in infamy”, versus “72 years and 2 months ago”. It can also be useful to know that an article, forum post, or comment, was published “2 hours ago”, but that usefulness diminishes when days, weeks, months, and even years pass.

There is also the question of accuracy with relative date language. We don’t often seek for exactness, but we do want things close. Certainly it wasn’t exactly 87 years to the day, let alone the hour, that “our fathers brought forth on this continent a new nation…” when President Abraham Lincoln gave The Gettysburg Address and yet it was accurate enough to convey his point.

The more we looked at adding relative dates in 2.8 the more we realized they are a matter of taste and a matter of context. We cannot accurately predict when dates should be relative, when they should stop being relative, how much accuracy to display, or if you prefer fortnights over weeks. Naturally we have some defaults in place, but we are also giving you the ability to make these decisions on a tag-by-tag basis.

As board gamer I like to keep track of what games I’ve played, and when I play them. Since board games are a social activity I want to share with my friends what games I have been playing:

<ul>
{exp:channel:entries channel="game_diary" limit="5"}
    
<li>{entry_date format="%M %j, %Y"}Played {game}</li>
{/exp:channel:entries}
</ul

My friends will know I’ve been playing games with my kids when they see the following:

<ul>
    <
li>Feb92014Played Uno</li>
    <
li>Feb42014Played Tokaido</li>
    <
li>Feb22014Played Crazy Eights</li>
    <
li>Jan152014Played Crazy Eights</li>
    <
li>Dec312013Played Crazy Eights</li>
</
ul

That’s not bad, but I would like my friends to see a relative date and after two weeks a standard date is fine. I’ll add a :relative modifier and a stop= parameter like so:

<li>{entry_date:relative stop="+2 weeks" format="%M %j, %Y"}Played {game}</li

Now my friends will see:

<ul>
    <
li>2 days agoPlayed Uno</li>
    <
li>7 days agoPlayed Tokaido</li>
    <
li>one week agoPlayed Crazy Eights</li>
    <
li>Jan152014Played Crazy Eights</li>
    <
li>Dec312013Played Crazy Eights</li>
</
ul

Unfortunatley I don’t have a regular gaming schedule, so it would be good to be able to communicate to my friends the next few times I’m available to play:

<ul>
{exp:channel:entries channel="game_sessions" limit="3" show_future_entries="yes"}
    
<li>Playing some board games {entry_date:relative}</li>
{/exp:channel:entries}
</ul

My friends will now see my next three board game nights:

<ul>
    <
li>Playing some board games in 6 hours</li>
    <
li>Playing some board games in 2 days</li>
    <
li>Playing some board games in about 2 weeks</li>
</
ul

This doesn’t feel accurate enough, especially if I allow people to drop by unannounced. I want to show more of the date:

<li>Playing some board games {entry_date:relative depth="2"}</li

Now they will see:

<ul>
    <
li>Playing some board games in 6 hours and 33 minutes</li>
    <
li>Playing some board games in 2 days and 7 hours</li>
    <
li>Playing some board games in one week and 6 days</li>
</
ul

Relative dates began as a fix for a bug in which the Wiki module did not have access to the {current_time} global variable. While adding that in we took the time to refactor all our date parsing and processing code. We have put a lot of thought into this feature. We hope you will enjoy it, and we look forward to seeing how you take advantage of relative date formats.

.(JavaScript must be enabled to view this email address) or share your feedback on this entry with @ellislab on Twitter.