Search

Anyone ever tried to convert a time to UTC before?

I'm trying to modify the default date-time utility, but the math to convert a time is kind of complicated around the beginning/end of a day.

Don't re-invent the wheel. I suggest to either do it in PHP or use EXSLT's date and time functions.

My use case has changed, since I was able to make the receiving application accept time-zoned dates.

But just for knowledge's sake, how would you do it in XSLT? I was looking at the EXSLT documentation today, and I had no idea what to do. If you open up the date:date-time() docs, it just says

function syntax

    string date:date-time()

not very helpful…

Here's a starter which will output date and time:

<?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"
    extension-element-prefixes="date">

<xsl:template match="/">
    <data>
        <date>
            <xsl:value-of select="date:date-time()"/>
        </date>
    </data>
</xsl:template>

</xsl:stylesheet>

Output:

<?xml version="1.0"?>
<data>
    <date>2013-09-05T12:14:09+02:00</date>
</data>

As you see, you must include the EXSLT's date namespace in the xsl:stylesheet element. Then you can use EXSLT dates and times functions.

To be precise: Only functions which are actually implemented in libxslt will work. You will find information about supporting processors at the bottom of each documentation page ("Implementations").

The funny stuff ist date calculations, of course (date:difference). I used them intensively once, and they really worked perfectly! :-)

It looks like date:add() is what I would need, but the documentation does not mention how it works. It says to follow the duration format here, but that link does not specify a format.

The link in the EXSLT docs is indeed helpful:

An optional preceding minus sign ('-') is allowed, to indicate a negative duration. If the sign is omitted a positive duration is indicated.

For example, to indicate a duration of 1 year, 2 months, 3 days, 10 hours, and 30 minutes, one would write: P1Y2M3DT10H30M. One could also indicate a duration of minus 120 days as: -P120D.

So why don't you try P2H (just an example, 2 correlates to my timezone)?

If you don't feel comfortable with doing that stuff in (E)XSLT, have you thought about writing a Symphony (PHP) extension that suits your needs?

I was able to use regular ISO dates with timezones in the end, so I don't have this problem anymore :)

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