Doc #71108 [Opn->Csd]: date_create() is NOT an alias for "new DateTime()"

[email protected] Thu, 02 Jun 2022 11:18:17 +0000
Newsgroups php.doc.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=71108&edit=1

 ID:                 71108
 Updated by:         [email protected]
 Reported by:        public at grik dot net
 Summary:            date_create() is NOT an alias for "new DateTime()"
-Status:             Open
+Status:             Closed
 Type:               Documentation Problem
 Package:            Date/time related
 Operating System:   irrelevant
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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()'


Previous Comments:
------------------------------------------------------------------------
[2015-12-13 10:46:26] 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



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=71108&edit=1