Search

Ruby on Rails has a handy function time_ago_in_words() to compare two dates and return the difference as words; “about one minute ago”, “2 hours ago”, “4 months from now” and so on.

I’ve ported this to an XSLT template for use within Symphony and added support for dates in the future as well as the past.

Download

http://www.getsymphony.com/downloads/xslt/file/20484/

Usage

Add the time-ago.xsl stylesheet to your Utilities and include it in the normal way. Call the time-ago template passing a valid ISO date (YYYY-MM-DDThh:mm:ss):

    <xsl:call-template name="time-ago">
        <xsl:with-param name="date-and-time" select="concat(publish-date, 'T', publish-date/@time, ':00')" />
    </xsl:call-template>

In the example above I have a Date field named “Publish Date”. I could have made the template accept just the date XML node itself, but I wanted to use this template in cases where dates are annoyingly stored as Text fields as well (and therefore not formatted with the time attribute in the XML).

Dependencies

  • Uses the math and date functions from the EXSLT library (note the stylesheet namespaces)
  • Symphony 2.0.1 ($current-time in the Params list)

Awesome, Nick, thanks a bunch.

Nick, this is so sweet!

Just wait for the any-number-to-words template that's coming :-)

Thanks! I tried to do this a while ago, but failed miserably. I'm checking this out. :-)

Fixed a bug whereby the number appeared when the "about a year" condition was met. Gist updated.

Seems to have been moved to http://www.getsymphony.com/downloads/xslt/file/20484/. Thanks Nick, works like a charm.

Updated the link. Glad it works for you :-)

This forum uses a modified version for the “Last Active” time in the discussion list.

To incorporate timezone differences you can modify the “now” variable: <xsl:variable name="now" select="concat($today, 'T', $current-time, ':00', $timezone)" />

This fixed a problem where I was importing a feed including a published date with time zone -07:00 in my website with time zone +02:00, and the time was off by 2 hours. Apparently, XSL assumes a time zone of +00:00 by default.

Just added some extra niceness for this script:

http://getsymphony.com/downloads/xslt/file/25156/

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details