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: Closed Package: Date/time related
PHP Version: Irrelevant OS: irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: public at grik dot net
New email:
PHP Version: OS:

 

 [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
 [2022-06-02 11:18 UTC] git@php.net
Automatic comment on behalf of derickr
Revision: https://github.com/php/doc-en/commit/fc1c548fabb0c2e411ee2f7984a741a805d4b030
Log: Fixed bug #71108: date_create() is NOT an alias for 'new DateTime()'
 [2022-06-02 11:18 UTC] git@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 13:01:29 2024 UTC