|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-01-21 20:21 UTC] 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. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 15:00:02 2025 UTC |
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