Edit report at https://bugs.php.net/bug.php?id=76848&edit=1
ID: 76848
Updated by: requinix@php.net
Reported by: wojciech dot mocek at gmail dot com
Summary: Possible to create Datetime from wrong month day
-Status: Open
+Status: Not a bug
Type: Bug
Package: Date/time related
Operating System: Linux
PHP Version: 7.1.21
Block user comment: N
Private report: N
New Comment:
That's right, it does not validate the date. It tries its best to come up with the most
reasonable interpretation of the string passed to it. Just like how strtotime does.
Previous Comments:
------------------------------------------------------------------------
[2018-09-07 11:17:36] wojciech dot mocek at gmail dot com
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 this bug report at https://bugs.php.net/bug.php?id=76848&edit=1