Bug #76848 [NEW]: Possible to create Datetime from wrong month day

From: Date: Fri, 07 Sep 2018 11:17:36 +0000
Subject: Bug #76848 [NEW]: Possible to create Datetime from wrong month day
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-216917@lists.php.net to get a copy of this message
From: wojciech dot mocek at gmail dot com Operating system: Linux PHP version: 7.1.21 Package: Date/time related Bug Type: Bug Bug description:Possible to create Datetime from wrong month day Description: ------------ Problem: Possible to create Datetime from wrong month day. Example: 1. new \Datetime('2018-09-31') => gives 2018-10-01 2. new \Datetime('2018-02-31') => gives 2018-03-03 and it applies to all months having less than 31 days. There is no such date like 31 of September or 31 of February, or 31 of November, and so on, while PHP library parses such invalid date string and creates Datetime object. Test script: --------------- $ php <<< "<?php var_dump(new \Datetime('2018-09-31'), new \Datetime('2018-02-31'));" -:1: class DateTime#1 (3) { public $date => string(26) "2018-10-01 00:00:00.000000" public $timezone_type => int(3) public $timezone => string(13) "Europe/Berlin" } -:1: class DateTime#2 (3) { public $date => string(26) "2018-03-03 00:00:00.000000" public $timezone_type => int(3) public $timezone => string(13) "Europe/Berlin" } Expected result: ---------------- Expected behaviour: It should not be possible to create \Datetime object using invalid day of month. $ php <<< "<?php var_dump(new \Datetime('2018-09-31'));" PHP Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string (2018-09-31) at position 9 (2): Unexpected character in -:1 Stack trace: #0 -(1): DateTime->__construct('2018-09-31') #1 {main} thrown in - on line 1 Actual result: -------------- $ php <<< "<?php var_dump(new \Datetime('2018-09-31'), new \Datetime('2018-02-31'));" -:1: class DateTime#1 (3) { public $date => string(26) "2018-10-01 00:00:00.000000" public $timezone_type => int(3) public $timezone => string(13) "Europe/Berlin" } -:1: class DateTime#2 (3) { public $date => string(26) "2018-03-03 00:00:00.000000" public $timezone_type => int(3) public $timezone => string(13) "Europe/Berlin" } -- Edit bug report at https://bugs.php.net/bug.php?id=76848&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=76848&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=76848&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=76848&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=76848&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=76848&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=76848&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=76848&r=needscript Try newer version: https://bugs.php.net/fix.php?id=76848&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=76848&r=support Expected behavior: https://bugs.php.net/fix.php?id=76848&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=76848&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=76848&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=76848&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=76848&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=76848&r=dst IIS Stability: https://bugs.php.net/fix.php?id=76848&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=76848&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=76848&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=76848&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=76848&r=mysqlcfg

« previous php.bugs (#216917) next »