Req #64923 [Opn->Sus]: DateTime::CreateFromFormat return false
| From: | cmb@php.net | Date: | Tue, 27 Oct 2020 11:36:57 +0000 |
| Subject: | Req #64923 [Opn->Sus]: DateTime::CreateFromFormat return false | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-229942@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=64923&edit=1
ID: 64923
Updated by: cmb@php.net
Reported by: ostico at gmail dot com
Summary: DateTime::CreateFromFormat return false
-Status: Open
+Status: Suspended
Type: Feature/Change Request
Package: Class/Object related
Operating System: Linux
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Well, returning an "empty" DateTime object on failure seems like a
very bad idea to me. Throwing an exception might make sense, but
that would certainly constitute BC break, and as such needs to be
discussed on the internals mailing list[1]. If you're still
interested in this feature, please forward your request to this
list. For the time being, I'm suspending this ticket.
[1] <https://www.php.net/mailing-lists.php#internals>
Previous Comments:
------------------------------------------------------------------------
[2013-05-25 23:31:32] ostico at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/datetime.createfromformat#refsect1-
datetime.createfromformat-returnvalues
---
I think that returning false is a bad behaviour that lead to surely unwanted
fatal exception when passing to it not well formatted dates.
This static method seems to be thought to get a datetime object on which method
format can be called inline:
DateTime::CreateFromFormat()->format()
But on not well formatted date strings the 'format' method raise an exception
E_ERROR because it is called on a boolean.
In my opinion CreateFromFormat should raise a throwable exception ( Best
Solution ) or return an
empty DateTime Object ( Unix Time 1970-01-01 ) and raise a warning, NOT a
boolean value.
Test script:
---------------
php > var_dump( DateTime::CreateFromFormat('Y-m-d', '2013-05-') );
php > try { DateTime::CreateFromFormat('Y-m-d', '2013-05-'); } catch (
Exception $e ) { var_dump( 'ok' ); }
php > try { var_dump( DateTime::CreateFromFormat('Y-m-d',
'2013-05-')->format('dmY') ); } catch ( Exception $e ) { echo 'ok';
}
Expected result:
----------------
PHP Warning: Uncaught exception 'Exception' in php shell code:1
string(2) "ok"
string(2) "ok"
//---------------
//alternatively the example should return an empty Datetime Object and
raise a warning
PHP Warning: Malformed Date String in php shell code on line 1
object(DateTime)#10 (3) {
["date"]=>
string(19) "1970-01-01 00:00:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/Berlin"
}
PHP Warning: Malformed Date String in php shell code on line 1
PHP Warning: Malformed Date String in php shell code on line 1
string(8) "01011970"
Actual result:
--------------
bool(false)
PHP Fatal error: Call to a member function format() on a non-object in php shell
code on line 1
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64923&edit=1