Skip to content

Instantly share code, notes, and snippets.

@xpathr
Created May 10, 2012 14:42
Show Gist options
  • Select an option

  • Save xpathr/2653570 to your computer and use it in GitHub Desktop.

Select an option

Save xpathr/2653570 to your computer and use it in GitHub Desktop.
Time Ago by nickdunn
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://exslt.org/dates-and-times" xmlns:math="http://exslt.org/math" extension-element-prefixes="date math">
<!--
Name: Time Ago
Version: 1.3
Author: Nick Dunn <nick@nick-dunn.co.uk>
URL: http://symphony-cms.com/downloads/xslt/file/20484/
Parameters:
* date-and-time (required) an ISO date in the format YYYY-MM-DDThh:mm:ss
-->
<xsl:template name="time-ago">
<xsl:param name="date-and-time"/>
<xsl:variable name="now" select="concat($today, 'T', $current-time, ':00')" />
<xsl:variable name="minutes" select="date:seconds(date:difference($date-and-time, $now)) div 60" />
<xsl:variable name="delta-minutes" select="math:abs($minutes)" />
<xsl:variable name="delta-in-words">
<xsl:choose>
<xsl:when test="$delta-minutes &lt; 1">
<xsl:text>less than a minute</xsl:text>
</xsl:when>
<xsl:when test="$delta-minutes = 1">
<xsl:text>one minute</xsl:text>
</xsl:when>
<xsl:when test="$delta-minutes &lt; 45">
<xsl:call-template name="time-ago-minutes">
<xsl:with-param name="minutes" select="$minutes"/>
</xsl:call-template>
<xsl:text> minutes</xsl:text>
</xsl:when>
<xsl:when test="$delta-minutes &lt; 90">
<xsl:text>about an hour</xsl:text>
</xsl:when>
<xsl:when test="$delta-minutes &lt; 1440">
<xsl:call-template name="time-ago-minutes">
<xsl:with-param name="minutes" select="floor($delta-minutes div 60)"/>
</xsl:call-template>
<xsl:text> hours</xsl:text>
</xsl:when>
<xsl:when test="$delta-minutes &lt; 2880">
<xsl:text>one day</xsl:text>
</xsl:when>
<xsl:when test="$delta-minutes &lt; 43200">
<xsl:call-template name="time-ago-minutes">
<xsl:with-param name="minutes" select="floor($delta-minutes div 1440)"/>
</xsl:call-template>
<xsl:text> days</xsl:text>
</xsl:when>
<xsl:when test="$delta-minutes &lt; 86400">
<xsl:text> about a month</xsl:text>
</xsl:when>
<xsl:when test="$delta-minutes &lt; 525600">
<xsl:call-template name="time-ago-minutes">
<xsl:with-param name="minutes" select="floor($delta-minutes div 43200)"/>
</xsl:call-template>
<xsl:text> months</xsl:text>
</xsl:when>
<xsl:when test="$delta-minutes &lt; 1051200">
<xsl:text>about a year</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="time-ago-minutes">
<xsl:with-param name="minutes" select="floor($delta-minutes div 525600)"/>
</xsl:call-template>
<xsl:text> years</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$delta-in-words"/>
<xsl:choose>
<xsl:when test="$minutes &lt; 0">
<xsl:text> from now</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text> ago</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="time-ago-minutes">
<xsl:param name="minutes"/>
<xsl:value-of select="$minutes"/>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment