Author:
nickdunn
Version:
1.0
Release Date:
5 May 2009
Category:
Date & Time

Description

A port of the RoR time_ago_in_words() function to XSLT. When supplied with an ISO date (past or future) it will return a human-readable string e.g. “about one minute ago”, “2 hours ago”, “4 months from now” and so on.

XSLT

View Raw
<?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://www.getsymphony.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>

Discuss this XSLT Utility

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