|  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
Avg. Score:3.8 ± 1.5
Reproduced:12 of 12 (100.0%)
Same Version:8 (66.7%)
Same OS:6 (50.0%)
From: ostico at gmail dot com Assigned:
Status: Suspended Package: Class/Object related
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2013-05-25 23:31 UTC] ostico at gmail dot com
From manual page:
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:

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) {
  string(19) "1970-01-01 00:00:00"
  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:
PHP Fatal error:  Call to a member function format() on a non-object in php shell 
code on line 1


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-10-27 11:36 UTC]
-Status: Open +Status: Suspended
 [2020-10-27 11:36 UTC]
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] <>
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jul 13 17:01:30 2024 UTC