[php-src] Issue #9701: DateTime::createFromFormat for ISO8601 resolves to false
| From: | TacticsJan | Date: | Mon, 10 Oct 2022 11:02:42 +0000 |
| Subject: | [php-src] Issue #9701: DateTime::createFromFormat for ISO8601 resolves to false | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-242559@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/9701
Author: TacticsJan
### Description
The following code:
```php
<?php
$date1 = DateTimeImmutable::createFromFormat('Y-m-d', '2022-10-18');
$date2 = DateTimeImmutable::createFromFormat('Y-m-d\TH:i:sP',
$date1->format('c'));
echo 'Date create from format "Y-m-d\TH:i:sP": ' .
$date2->format('c') . PHP_EOL;
echo PHP_EOL;
$date3 = DateTimeImmutable::createFromFormat('c', $date1->format('c'));
echo print_r(DateTimeImmutable::getLastErrors());
echo 'Date create from format "c": ' . $date3->format('c') .
PHP_EOL;
```
Resulted in this output:
```
Original Date: 2022-10-18T10:40:59+00:00
Date create from format "Y-m-d\TH:i:sP": 2022-10-18T10:40:59+00:00
Array
(
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 2
[errors] => Array
(
[0] => The format separator does not match
[1] => Trailing data
)
)
1
Fatal error: Uncaught Error: Call to a member function format() on bool in
/home/user/scripts/code.php:17
Stack trace:
#0 {main}
thrown in /home/user/scripts/code.php on line 17
```
But I expected this output instead:
```
Date create from format "Y-m-d\TH:i:sP": 2022-10-18T10:50:53+00:00
Array
(
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
)
1Date create from format "c": 2022-10-18T10:50:53+00:00
```
It's weird to have the possibility to format to ISO8601 using ->format('c'); but
createFromFormat is not able to resolve format 'c' to a valid DateTime.
### PHP Version
PHP 8.1.11
### Operating System
_No response_