php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44383 PHP DateTime not converted to xsd:datetime
Submitted: 2008-03-09 18:19 UTC Modified: 2023-10-14 15:51 UTC
Votes:86
Avg. Score:4.6 ± 0.8
Reproduced:72 of 73 (98.6%)
Same Version:45 (62.5%)
Same OS:37 (51.4%)
From: kevin dot craft at gmail dot com Assigned: nielsdos (profile)
Status: Closed Package: SOAP related
PHP Version: 7.1.7 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
42 - 22 = ?
Subscribe to this entry?

 
 [2008-03-09 18:19 UTC] kevin dot craft at gmail dot com
Description:
------------
When using a SoapServer object in WSDL mode, PHP DateTime objects are converted to an empty xsd:datetime element. To produce the correct results, DateTime objects have to be formatted as strings, which defeats the purpose of using the SoapServer to map data types.

Environment:
------------
O/S: Windows XP
Web Server: Apache 2.2
PHP: 5.2.5 w/ GD, MySQL, and SOAP

Reproduce code:
---------------
bug.wsdl:

    <?xml version ="1.0" encoding ="UTF-8" ?> 
    <wsdl:definitions name="GetCurrentDate" 
      targetNamespace="http://localhost"
      xmlns:tns="http://localhost"
      xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
      xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

      <wsdl:message name="GetCurrentDateRequest" />
      <wsdl:message name="GetCurrentDateResponse">
        <wsdl:part name="currentDate" type="xsd:datetime" />
      </wsdl:message>
  
      <wsdl:portType name="GetCurrentDatePortType">
        <wsdl:operation name="getCurrentDate">
          <wsdl:input message="tns:GetCurrentDateRequest" />
          <wsdl:output message="tns:GetCurrentDateResponse" />
        </wsdl:operation>
      </wsdl:portType>

      <wsdl:binding name="GetCurrentDateBinding" type="tns:GetCurrentDatePortType">
        <soap:binding style="rpc"
          transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name="getCurrentDate">
          <soap:operation soapAction="http://localhost#getCurrentDate" />
          <wsdl:input>
            <soap:body use="encoded"
              namespace="urn:xmethods-delayed-quotes"
          encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
          </wsdl:input>
          <wsdl:output>
            <soap:body use="encoded"
              namespace="urn:xmethods-delayed-quotes"
              encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
          </wsdl:output>
        </wsdl:operation>
      </wsdl:binding>

      <wsdl:service name="GetCurrentDateService">
        <wsdl:port name="GetCurrentDatePortType" binding="GetCurrentDateBinding">
          <soap:address location="http://localhost" />
        </wsdl:port>
      </wsdl:service>
    </wsdl:definitions>

server.php:

    <?php
      function getCurrentDate() {
        return new DateTime();
      }

      $server = new SoapServer('bug.wsdl');
      $server->addFunction('getCurrentDate');
      $server->handle();
    ?>

client.php

    <?php
      $client = new SoapClient('bug.wsdl', array('trace' => 1));
      echo 'current date: '.$client->getCurrentDate().'<br /><br />';
      echo 'last response: '.htmlentities($client->__getLastResponse());
    ?>

Expected result:
----------------
Assuming the current date is 2008-03-08 23:00:00:

current date: 2008-03-06 00:00:00 (or any other format accepted by xsd:datetime)

last response: <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:xmethods-delayed-quotes" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:getCurrentDateResponse><currentDate xsi:type="xsd:datetime">2008-03-06 00:00:00</currentDate></ns1:getCurrentDateResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

Actual result:
--------------
current date:

last response: <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:xmethods-delayed-quotes" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:getCurrentDateResponse><currentDate xsi:type="xsd:datetime" /></ns1:getCurrentDateResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-09 18:22 UTC] kevin dot craft at gmail dot com
In case it wasn't obvious, "Expected Result" should have read: "Assuming the current date is 2008-03-06 00:00:00:" rather than "2008-03-08 23:00:00".
 [2008-03-15 13:06 UTC] felipe@php.net
Your idea then is add __toString in DateTime?
This is not a bug, but a "feature request". :)
 [2008-03-16 07:09 UTC] derick@php.net
We can't add __toString() to the date class, as it would have no idea on which format to use to render it. So it should be done in the SOAP ext.
 [2008-03-17 01:17 UTC] kevin dot craft at gmail dot com
Exactly. The SOAP extension should know what format to output the DateTime as because it should be specified as a W3C standard somewhere in the SOAP or XML Schema definition. When the SOAP extension encounters an xsd:datetime (or related type), it should output the DateTime object according to the standard for that type.
 [2008-05-13 04:40 UTC] barth at pbx-network dot de
Anyone has a soluiton or workaround for this issue?  How can a date/time been passed over to a webservice endpoint?
 [2008-06-30 12:00 UTC] r dot janssen at keensystems dot eu
I am, too, looking for a solution for this problem.
I can specify parameters as dateTime type but when generating the WSDL the generation stops and does nothing.
 [2009-06-29 08:28 UTC] david dot zuelke at bitextender dot com
We've created a patch to implement this.

Description (with patch and tests for download):
http://article.gmane.org/gmane.comp.php.devel/57369

Patch (in case gmane doesn't work):
http://pastie.org/527755

Tests (in case gmane doesn't work):
http://pastie.org/527762
 [2009-06-29 08:56 UTC] lsmith@php.net
Reopening since we now have a patch.
 [2009-08-07 15:23 UTC] david dot zuelke at bitextender dot com
Updated patch and tests: http://pastie.org/575559
 [2010-10-18 17:43 UTC] aldekein at myevil dot info
It still does not work after 2.5 years in PHP 5.3.1 on Windows.

Maybe this patch should be applied to official PHP branch?
 [2012-01-25 19:44 UTC] frozenfire@php.net
I've encountered this issue today, and it would be really wonderful to have this 
patch applied.
 [2012-05-03 10:33 UTC] andyidol at gmail dot com
Please fix this! I beg you )
 [2012-12-24 07:29 UTC] mj@php.net
David, I applied your patch to 5.6.0-dev but all the new tests are failing due to 
empty responses from SoapServer. Do you have 
some cycles to look into this?
 [2012-12-24 07:29 UTC] mj@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: mj
 [2013-01-21 18:37 UTC] thomas dot lallement at 9online dot fr
For me, when you call $client->getCurrentDate(), the expected result would be to have a PHP DateTime object rather than a string.

So the expected result should be:

DateTime Object
(
    [date] => 2008-03-06 00:00:00
    [timezone_type] => x
    [timezone] => xxxx/xxxxx
)

What do you think about this? At least an option could be provide throught the SoapClient to permit this behavior.
 [2013-02-18 00:33 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.
 [2013-02-28 13:01 UTC] stefan dot giesecke at avl-investmentfonds dot de
Please fix this issue, prefer the example from  thomas dot lallement at 9online dot fr. Optional or not optional I don't care.
 [2013-08-31 19:05 UTC] eric dot dorr at de-cix dot net
please fix this important SOAP feature.
 [2016-12-19 19:50 UTC] contact at shaunoneil dot eu
Is there at least a workaround for this?  As it stands, if the remote API expects an xsd:dateTime,  my only option is a different language?
 [2017-07-18 16:34 UTC] f dot rauch at outlook dot com
This still seems to be an issue with PHP 7, regardless of OS. Has there been any progress so far? This bug does make it impossible to properly work with the SOAP package if the API one is working with requires proper xsd:dateTime values.
 [2017-07-18 16:38 UTC] requinix@php.net
-Status: No Feedback +Status: Re-Opened -PHP Version: 5.*, 6 (2009-08-07 +PHP Version: 7.1.7 -Assigned To: mj +Assigned To:
 [2023-07-14 12:24 UTC] hanmac at gmx dot de
The Bug still exist to this day in PHP 8.2+



my current workaround is using the typemap option for SOAP Client:


    protected static array $typeMap = [
        [
            "type_ns"  => "h**p://www.w3.org/2001/XMLSchema", 
            "type_name" => "date",
            "to_xml"    => [self::class, 'toXSDDate']
        ]
    ];


    /**
     * SOAP Bug with Datetime https://bugs.php.net/bug.php?id=44383
     *
     * @param DateTimeInterface|null $dateTime
     * @return string|null
     */
    public static function toXSDDate(?DateTimeInterface $dateTime): ?string
    {
        if (is_null($dateTime)) {
            return null;
        }
        return $dateTime->format(DateTimeInterface::ATOM);
    }
 [2023-07-14 15:58 UTC] hanmac at gmx dot de
because it is nowhere documented, but typemap to_xml needs a valid xml string, otherwise it will silently do nothing

there is my fixed example

function toXSDDate(?DateTimeInterface $dateTime) : ?string
{
  return '<a>' . $dateTime->format(DateTimeInterface::ATOM) . '</a>';
}
 [2023-10-14 15:51 UTC] nielsdos@php.net
-Assigned To: +Assigned To: nielsdos
 [2023-10-14 15:51 UTC] nielsdos@php.net
I'm looking into this now
 [2023-10-18 17:25 UTC] nielsdos@php.net
There's an implementation PR for this now: https://github.com/php/php-src/pull/12437
Feel free to test
 [2023-12-08 16:27 UTC] git@php.net
Automatic comment on behalf of nielsdos
Revision: https://github.com/php/php-src/commit/b34b4d54c362e8efa26e7ad0f8c017c6d3cfce92
Log: Fix #44383: PHP DateTime not converted to xsd:datetime
 [2023-12-08 16:27 UTC] git@php.net
-Status: Re-Opened +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 00:01:27 2024 UTC