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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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: Sun Dec 22 01:01:30 2024 UTC