php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #71108 date_create() is NOT an alias for "new DateTime()"
Submitted: 2015-12-13 10:46 UTC Modified: 2017-01-28 13:02 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: public at grik dot net Assigned:
Status: Open Package: Date/time related
PHP Version: Irrelevant OS: irrelevant
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2015-12-13 10:46 UTC] public at grik dot net
Description:
------------
---
From manual page: http://www.php.net/function.date-create
---

date_create() is NOT an alias, It is closer to be named a factory.

For the first hand, DateTime::__construct() can't be called, which is obvious.

For another, there is a significant difference between date_create() and "new DateTime()"

We write 
if (!($DateTime = date_create('garbage'))){
//process error
}
but 
try {
  $DateTime = new DateTime('garbage');
}catch(Exception $E){
...
}


Test script:
---------------
$ php -r 'var_dump(date_create("garbage"));'
bool(false)
$ php -r 'var_dump(new DateTime("garbage"));'

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (garbage) at position 0 (s): The timezone could not be found in the database' in Command line code:1
Stack trace:
#0 Command line code(1): DateTime->__construct('garbage')
#1 {main}
thrown in Command line code on line 1


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-01-28 13:02 UTC] cmb@php.net
-Package: Documentation problem +Package: Date/time related
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Dec 02 04:01:24 2020 UTC