Search

Hey guys,

What would be the easiest way to compare dates using xslt?

I've got for example today date and i want to check if the date falls between 2 dates?!

Example:

<xsl:variable name="start-date" select="date:date(concat($this-year, '-07-01'))" />
<xsl:variable name="end-of-year" select="date:date(concat($this-year, '-12-31'))" />

<xsl:if test="$today-date &gt; $start-date and $today-date &lt; $end-of-year" >
   ????
</xsl:if>   

Thanks all, Andre.

There's also this small utility, in case you need an extra check on the validity/compliance of a date.

Thanks guys, i had another approach. Not sure if its the best one however its been working.

    <xsl:variable name="today-date" select="number(translate(substring(date:date(),1,10),'-',''))"/>

    <!-- First trimester -->
    <xsl:variable name="start-of-year-1" select="number(translate(substring(date:date(concat($this-year, '-01-01')),1,10),'-',''))"/>
    <xsl:variable name="end-of-year-1" select="number(translate(substring(date:date(concat($this-year, '-03-30')),1,10),'-',''))"/>

    <!-- Second trimester -->
    <xsl:variable name="start-of-year-2" select="number(translate(substring(date:date(concat($this-year, '-04-01')),1,10),'-',''))"/>
    <xsl:variable name="end-of-year-2" select="number(translate(substring(date:date(concat($this-year, '-06-30')),1,10),'-',''))"/>

    <!-- Third Trimester -->
    <xsl:variable name="start-of-year-3" select="number(translate(substring(date:date(concat($this-year, '-07-01')),1,10),'-',''))"/>
    <xsl:variable name="end-of-year-3" select="number(translate(substring(date:date(concat($this-year, '-12-31')),1,10),'-',''))"/>

    <xsl:variable name="percentage">
       <xsl:choose>
         <xsl:when test="$url-shop != 'yes' and $today-date &gt;= $start-of-year-1 and $today-date &lt;= $end-of-year-1">0.75</xsl:when>
         <xsl:when test="$url-shop != 'yes' and $today-date &gt;= $start-of-year-2 and $today-date &lt;= $end-of-year-2" >0.50</xsl:when>
         <xsl:when test="$url-shop != 'yes' and $today-date &gt;= $start-of-year-3 and $today-date &lt;= $end-of-year-3">1</xsl:when>
         <xsl:otherwise>1</xsl:otherwise>
       </xsl:choose>
    </xsl:variable>

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