Bug #64040 [Com]: DateTime::createFromFormat() incorrectly parses illogical dates
[email protected] ("php-bugs at allenjb dot me dot uk")
| Newsgroups | php.bugs |
|---|---|
| Message-ID | <[email protected]> |
Edit report at https://bugs.php.net/bug.php?id=64040&edit=1
ID: 64040
Comment by: php-bugs at allenjb dot me dot uk
Reported by: tagadvance at gmail dot com
Summary: DateTime::createFromFormat() incorrectly parses
illogical dates
Status: Open
Type: Bug
Package: Date/time related
Operating System: Linux
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
DateTime does issue a warning through its own internal warning system (DateTime->getLastErrors()) and always has done as far as I can see: https://3v4l.org/uGDe7
Previous Comments:
------------------------------------------------------------------------
[2014-06-25 13:19:25] nicodoggie at gmail dot com
Taking the same code as OP, another example of parsing illogical dates:
<?php
// November 16 falls on a Friday, NOT a Wednesday
$date = '2014-22-01';
$format = 'Y-m-d';
try {
$dateTime = DateTime::createFromFormat($format, $date);
$formattedDateTime = $dateTime->format($format);
echo $formattedDateTime . PHP_EOL; // prints "2014-10-01"
} catch (Exception $e) {
// no exception is thrown =(
echo $e->getMessage();
}
?>
It would be better to throw out a warning, or at least give us a $useStrict = false parameter to play with so we don't break anyone's weird implementation.
Tested on ArchLinux PHP 5.5.13
------------------------------------------------------------------------
[2014-04-25 07:16:25] metamarkers at gmail dot com
"Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?"
There is no amount of code that can be written to error-correct the programmer.
------------------------------------------------------------------------
[2013-01-21 20:21:32] tagadvance at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/datetime.createfromformat
---
The DateTime::createFromFormat() method incorrectly parses an illogical date. I
have tested this on version 5.3.3 (CentOS) and 5.4.6 (Ubuntu).
Test script:
---------------
<?php
// November 16 falls on a Friday, NOT a Wednesday
$date = 'Wednesday 16 November 2012';
$format = 'l d M Y';
try {
$dateTime = DateTime::createFromFormat($format, $date);
$formattedDateTime = $dateTime->format($format);
echo $formattedDateTime . PHP_EOL; // prints "Wednesday 21 Nov 2012"
} catch (Exception $e) {
// no exception is thrown =(
echo $e->getMessage();
}
?>
Expected result:
----------------
I expect the createFromFormat method to throw an error if the date is illogical.
At the very least a warning should be added to the log.
Actual result:
--------------
The createFromFormat parses the illogical date. The day seems to take precedence
over the date. I am just guessing here, but it then seems to round up by one week
so that the date makes sense.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64040&edit=1