Reputation: 2040
I have an XML feed that I'm transforming using XSL. The date for each post from the XML comes in this format:
2011-03-09T10:44:27Z
I'd like to be able to convert it into "50 minutes ago" or "3 days ago" format, is this possible just using XSL or is PHP the 'only' option ?
Upvotes: 2
Views: 2109
Reputation:
With XSLT 1.0 use Jenny Tenison pure XSLT implementation of EXSLT date:difference()
.
As proof of concept, this stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times">
<xsl:import href="date.difference.template.xsl"/>
<xsl:template match="date">
<xsl:variable name="vDuration">
<xsl:call-template name="date:difference">
<xsl:with-param name="start" select="/test/@today" />
<xsl:with-param name="end" select="." />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="vDays"
select="substring-before(substring-after($vDuration,'P'),'D')"/>
<xsl:variable name="vHours"
select="substring-before(substring-after($vDuration,'T'),'H')"/>
<xsl:variable name="vMinutes"
select="substring-before(substring-after($vDuration,'H'),'M')"/>
<xsl:if test="$vDays">
<xsl:value-of select="concat($vDays,' days ')"/>
</xsl:if>
<xsl:if test="$vHours">
<xsl:value-of select="concat($vHours,' hours ')"/>
</xsl:if>
<xsl:if test="$vMinutes">
<xsl:value-of select="concat($vMinutes,' minutes ')"/>
</xsl:if>
<xsl:text>ago
</xsl:text>
</xsl:template>
</xsl:stylesheet>
With this input:
<test today="2011-03-09T15:00:00Z">
<date>2011-03-09T10:44:27Z</date>
<date>2011-02-09T10:44:27Z</date>
</test>
Output:
4 hours 15 minutes ago
28 days 4 hours 15 minutes ago
Upvotes: 7