Bug #76848 [Asn->Nab]: Possible to create Datetime from wrong month day

From: Date: Wed, 21 Nov 2018 15:10:06 +0000
Subject: Bug #76848 [Asn->Nab]: Possible to create Datetime from wrong month day
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-218076@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76848&edit=1

 ID:                 76848
 Updated by:         cmb@php.net
 Reported by:        wojciech dot mocek at gmail dot com
 Summary:            Possible to create Datetime from wrong month day
-Status:             Assigned
+Status:             Not a bug
 Type:               Bug
 Package:            Date/time related
 Operating System:   Linux
 PHP Version:        7.1.21
-Assigned To:        cmb
+Assigned To:        
 Block user comment: N
 Private report:     N

 New Comment:

Obviously,

  new DateTime('2018-01-31 +1 month')

and

  new DateTime('2018-02-31')

should both evaluate to the same value, shouldn't they?


Previous Comments:
------------------------------------------------------------------------
[2018-11-21 15:05:30] wojciech dot mocek at gmail dot com

What is the correlation between your question and reported bug?

------------------------------------------------------------------------
[2018-11-21 12:43:28] cmb@php.net

And how should the following be handled?

  new DateTime('2018-01-31 +1 month')

------------------------------------------------------------------------
[2018-11-21 11:06:29] wojciech dot mocek at gmail dot com

Open.

------------------------------------------------------------------------
[2018-09-11 11:44:56] wojciech dot mocek at gmai dot com

You wrote:
1. "That's right, it does not validate the date."

Well, hmm, I just run script:
$ php <<< "<?php var_dump(new \Datetime('2018-09-32'), new
\Datetime('2018-02-32'));"
PHP Fatal error:  Uncaught Exception: DateTime::__construct(): Failed to parse time string
(2018-09-32) at position 9 (2): Unexpected character in Standard input code:1
Stack trace:
#0 Standard input code(1): DateTime->__construct('2018-09-32')
#1 {main}
  thrown in Standard input code on line 1

So it looks that is validates given input somehow.

2. "It tries its best to come up with the most reasonable interpretation of the string passed
to it. Just like how strtotime does.". Come on, please ask people around to do something on
"2018-02-31", I think noone will think to do it on "2018-03-03" My question #2
is: do you say that if tell me "Send this mail on 2018-02-31" it is MOST REASONABLE to
interpret this as "send it on 2018-03-03" ?! 

3. "It tries its best to come up with the most reasonable interpretation of the string passed
to it. Just like how strtotime does." . You mentioned about strtotime:
$ php <<< "<?php var_dump(strtotime('2018-02-31'));"
int(1520035200)
$ php <<< "<?php var_dump(strtotime('2018-02-32'));"
bool(false)
$ php <<< "<?php var_dump(new \Datetime('2018-02-32'));"
PHP Fatal error:  Uncaught Exception: DateTime::__construct(): Failed to parse time string
(2018-02-32) at position 9 (2): Unexpected character in Standard input code:1
Stack trace:
#0 Standard input code(1): DateTime->__construct('2018-02-32')
#1 {main}
  thrown in Standard input code on line 1

 - 31 of February - there is no such date ever in calendar, but PHP creates time.
 - 32 of February - also there is no such date ever in calendar, PHP forbids to create time
"It tries its best to come up with the most reasonable interpretation of the string passed to
it" - if it is possible to create time from "never existing 31 of February", so why
it is not possible to create time from also "never existing 32 of February" ?

3. In normal life we cannot do anything on 31 of February, nor 31 of September. If you write
business application for invoicing, banks, anything, you must validate the date. And if someone
writes 31 of February to for example pay tax or send cash, it is mistake and NOT REASONABLE to
process. So "day of month" validation should be implemented. Otherwise all business
applications will have to use some custom PHP date library, only because native PHP allows to create
such nonsense like date from '2018-02-31'.

------------------------------------------------------------------------
[2018-09-08 03:10:55] requinix@php.net

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.

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=76848


--
Edit this bug report at https://bugs.php.net/bug.php?id=76848&edit=1


Thread (14 messages)

« previous php.bugs (#218076) next »