php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64923 DateTime::CreateFromFormat return false
Submitted: 2013-05-25 23:31 UTC Modified: 2020-10-27 11:36 UTC
Votes:11
Avg. Score:4.3 ± 1.1
Reproduced:11 of 11 (100.0%)
Same Version:7 (63.6%)
Same OS:6 (54.5%)
From: ostico at gmail dot com Assigned:
Status: Suspended Package: Class/Object related
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ostico at gmail dot com
New email:
PHP Version: OS:

 

 [2013-05-25 23:31 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-10-27 11:36 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2020-10-27 11:36 UTC] cmb@php.net
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>
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Oct 31 20:01:24 2020 UTC