Uploaded image for project: 'PHP Driver: Extension'
  1. PHP Driver: Extension
  2. PHPC-2429

Incorrect DateTime from UTCDateTime::toDateTime()

    • Type: Icon: Task Task
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 1.19.4
    • Affects Version/s: 1.18.0
    • Component/s: BSON
    • None
    • PHP Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

       

      var_dump(date_default_timezone_get());
      $date = new \DateTimeImmutable('1960-01-01 12:12:12.1');
      var_dump($date);
      
      $bsonDate = new \MongoDB\BSON\UTCDateTime($date);
      var_dump($bsonDate);
      
      $newDate = $bsonDate->toDateTime();
      var_dump($newDate);
      var_dump($newDate->format('Y-m-d H:i:s.u'));
      
      \Carbon\Carbon::instance($newDate);
      

      The last line generates an error while it should create a Carbon object with the correct date object.

       

      Output:

       

      string(3) "UTC"
      object(DateTimeImmutable)#2 (3) {
        ["date"]=>
        string(26) "1960-01-01 12:12:12.100000"
        ["timezone_type"]=>
        int(3)
        ["timezone"]=>
        string(3) "UTC"
      }
      object(MongoDB\BSON\UTCDateTime)#6 (1) {
        ["milliseconds"]=>
        string(13) "-315575267900"
      }
      object(DateTime)#7 (3) {
        ["date"]=>
        string(27) "1960-01-01 12:12:13.-900000"
        ["timezone_type"]=>
        int(1)
        ["timezone"]=>
        string(6) "+00:00"
      }
      string(27) "1960-01-01 12:12:13.-900000"
      
      PHP Fatal error:  Uncaught DateMalformedStringException: Failed to parse time string (1960-01-01 12:12:13.-900000) at position 24 (0): Unexpected character in vendor/nesbot/carbon/src/Carbon/Traits/Creator.php:100
      Stack trace:
      #0 vendor/nesbot/carbon/src/Carbon/Traits/Creator.php(100): DateTime->__construct('1960-01-01 12:1...', Object(Carbon\CarbonTimeZone))
      #1 vendor/nesbot/carbon/src/Carbon/Traits/Creator.php(149): Carbon\Carbon->__construct('1960-01-01 12:1...', Object(Carbon\CarbonTimeZone))
      #2 date.php(14): Carbon\Carbon::instance(Object(DateTime))
      #3 {main}Next Carbon\Exceptions\InvalidFormatException: Failed to parse time string (1960-01-01 12:12:13.-900000) at position 24 (0): Unexpected character in vendor/nesbot/carbon/src/Carbon/Traits/Creator.php:102
      Stack trace:
      #0 vendor/nesbot/carbon/src/Carbon/Traits/Creator.php(149): Carbon\Carbon->__construct('1960-01-01 12:1...', Object(Carbon\CarbonTimeZone))
      #1 date.php(14): Carbon\Carbon::instance(Object(DateTime))
      #2 {main} thrown in vendor/nesbot/carbon/src/Carbon/Traits/Creator.php on line 102

      Issue found while working on tests for PHPORM-234

       

            Assignee:
            andreas.braun@mongodb.com Andreas Braun
            Reporter:
            jerome.tamarelle@mongodb.com Jérôme Tamarelle
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: