Bug #74681 [Nab]: createFromFormat incorrect behavior if no day provided

From: Date: Wed, 31 May 2017 15:45:51 +0000
Subject: Bug #74681 [Nab]: createFromFormat incorrect behavior if no day provided
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-209328@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74681&edit=1 ID: 74681 User updated by: raul at reyes dot solutions Reported by: raul at reyes dot solutions Summary: createFromFormat incorrect behavior if no day provided Status: Not a bug Type: Bug Package: Date/time related Operating System: Any PHP Version: Irrelevant Block user comment: N Private report: N New Comment: oops, meant to change the t to a d ... thanks for letting me know about ! and | modifiers, Previous Comments: ------------------------------------------------------------------------ [2017-05-31 15:38:52] derick@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php By default, all the fields are prefilled in with the current year/month/day values. So my with 0218 becomes "2017-02-31", which overflows to 2017-03-03. You can prevent this by using the ! format specifier before "my" or the "|" specifier behind the "my" to set all values to 1970-01-01 00:00:00 first: <?php var_dump(DateTime::createFromFormat('!my',"0218")); var_dump(DateTime::createFromFormat('my|',"0218")); ?> your "Actual result" is not "2018-03-03" btw, it's "2018-03-31" as "t" is the number of days in the month (3), which is 31. ------------------------------------------------------------------------ [2017-05-31 15:14:04] raul at reyes dot solutions Description: ------------ Seems to default all dates to 31. if trying to create February of 2018 for example it defaults to March 3rd. DateTime::createFromFormat('my',"0218") sets the date to 2018-03-03. I would think the correct behavior is setting the date to 01 (or maybe the last day 't') Test script: --------------- $date = DateTime::createFromFormat('my',"0218"); echo $date->format('Y-m-t') . " when expecting 2018-02-01"; Expected result: ---------------- 2018-02-01 Actual result: -------------- 2018-03-03 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=74681&edit=1

« previous php.bugs (#209328) next »