|  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: -
Avg. Score:4.4 ± 1.0
Reproduced:10 of 10 (100.0%)
Same Version:6 (60.0%)
Same OS:5 (50.0%)
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
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

PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Jun 20 07:01:25 2019 UTC