Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    40
    Thanks
    16
    Thanked 0 Times in 0 Posts

    12h format to 24h format in XML

    Hi,
    The observation time is in 12h format. How to change it to 24h format?



    Working PHP:
    PHP Code:
    <?php 
    date_default_timezone_set
    ('UTC'); 
     
    $xslDoc = new DOMDocument(); 
    $xslDoc->load("xslfile.xsl"); 
     
    $xmlDoc = new DOMDocument(); 
    $xmlDoc->load("http://free.worldweatheronline.com/feed/weather.ashx?q=milan,italy&format=xml&num_of_days=5&key=af8b2fc417222733111712"); 
     
    $proc = new XSLTProcessor(); 
    $proc->importStylesheet($xslDoc); 
    echo 
    $proc->transformToXML($xmlDoc); 
    ?>


    Working XSL:
    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl">
    <xsl:template match="data">
    <html>
    <head><title></title></head>
    <body>
    <p>
    <xsl:apply-templates select="./current_condition" />
    </p>
    </body>
    </html>
    </xsl:template>
     
    <xsl:template match="current_condition">
    <td style="width: 96px">
    <p>Observation Time: <xsl:value-of select="./observation_time" /></p>
    <p><xsl:value-of select="./temp_C" />C</p>
    </td>
    </xsl:template>
     
    </xsl:stylesheet>
    Last edited by Spookster; 01-05-2012 at 08:30 PM. Reason: fixing code tags

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,369
    Thanks
    8
    Thanked 1,075 Times in 1,066 Posts
    They should just make a setting to allow users to select 24H or 12H time.
    You should email them and ask about it. That would be the easiest.
    It would be very easy for them to add that option.

    In the meantime, someone will have to give you a function to convert.
    I don't have the time right this moment.



    .
    Last edited by mlseim; 01-02-2012 at 09:05 PM.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    There are several ways to do this both XSLT and PHP side. The problem is really your use of XSLProcessor in PHP, which limits the XSLT options to that in the libxslt version.
    PHP wise you can use a formatter by running a custom function that takes the time, uses strtotime to convert it, and return it in the new format. This requires the use of the php: namespace in the XSLT though, but is more reliable than assuming that you have 2.0 available to use. One of your other threads (here: PHP XSLT for styling, but picture url in XML file gives a challenge) does just this purpose, but the name of the function may not make a lot of sense since I wrote it for the day of the week. The only thing different is that the format would instead be provided to it as 'h:s' instead of the 'l'.

    XSLT can use the format-date function (I think that's what it is, or date-format one of the two). The problem is the processor may not support that in PHP due to its version.
    There is a pure XSL solution I found here: http://stackoverflow.com/questions/7...r-time-in-xslt. That appears that it should work on a 1.0 version of XSLT as well.

    You could also force the dom to add the child node for the XSLT and pump the pure XML out instead. Pro would be that its the browser is now responsible for parsing it, while the con is the browser is responsible for parsing it. IMO, the vast majority of the browsers in use will support XML and XSLT parsing, so this is actually a feasible option to use.

  • Users who have thanked Fou-Lu for this post:

    JonesJ (01-03-2012)

  • #4
    New Coder
    Join Date
    Jul 2011
    Posts
    40
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Okay,
    It was more straight forward than could have been imagined.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •