Search

A new XSLT utility, “Time Ago (Friendly)” is now available for download. Comments and feedback can be left here but if you discover any issues, please post it on the issue tracker.

Can you show how this is used?

It’s invoked by the following:

<xsl:call-template name="time-ago" />
    <xsl:with-param name="date-and-time" select="path/to/node" />
</xsl:call-template>

The path/to/node needs to be replaced with a date and time node from your xml output. How you reference this depends upon where you are in the node tree.

Thanks, this is really, really, really useful!

What if after "23 hours ago" I no longer wanted to display relative time and instead show standard date "3/7/2011". How would I do this?

this is how i did it, but someone may be able to suggest a better way

<xsl:variable name="time-ago">
    <xsl:call-template name="time-ago">
        <xsl:with-param name="date-and-time" select="date" />
    </xsl:call-template>
</xsl:variable>

<xsl:if test="not(contains($time-ago, 'hours') or contains($time-ago, 'minutes') or contains($time-ago, 'seconds'))">
    <xsl:value-of select="date" />
</xsl:if>

<xsl:if test="contains($time-ago, 'hours') or contains($time-ago, 'minutes') or contains($time-ago, 'seconds')">
    <xsl:value-of select="$time-ago" />
</xsl:if>

another way of doing it may be to simply test your target date against $today and if it's not today, use the original date, otherwise use time-ago

Technically designermonkey's code above has a small error with an extra slash in the first call-template. Took me a little while to figure that out, here is the corrected code:

<xsl:call-template name="time-ago">
    <xsl:with-param name="date-and-time" select="path/to/node" />
</xsl:call-template>

Line 45 is unnecessary;

<xsl:value-of select="floor($delta-minutes div 10080)"/>

Thanks, brendo!

I believe this part

<xsl:when test="$delta-minutes &lt; 45">
   <xsl:value-of select="$minutes"/>
   <xsl:text> minutes</xsl:text>
</xsl:when>

should have floor($delta-minutes) instead of $minutes, otherwise it shows seconds as well.

For correct display in other timezones than GMT I suggest to add this line posted by carsten.

<xsl:variable name="now" select="concat($today, 'T', $current-time, ':00', $timezone)" />

I could supply a German translation  – would it make sense to publish it and if yes, where?

I was thinking about translations for this; too. However I'm not too convinced they should be in the XSL itself; maybe an accompanying datasource/xml/extension would make more sense to provide the textual translations.

Not sure if these are provided from within the symphony backend as translations since we have 'relative' time if so a datasource with symphony backend translations should work like a charm.

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