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: -
Votes:9
Avg. Score:4.3 ± 1.1
Reproduced:9 of 9 (100.0%)
Same Version:6 (66.7%)
Same OS:5 (55.6%)
From: ostico at gmail dot com Assigned:
Status: Open 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
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

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Mar 26 18:01:26 2019 UTC