|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78383 Casting a DateTime to array no longer returns its properties
Submitted: 2019-08-07 13:03 UTC Modified: 2019-08-08 08:23 UTC
From: jeremy at derusse dot com Assigned: nikic (profile)
Status: Closed Package: Date/time related
PHP Version: 7.4.0beta1 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
46 + 8 = ?
Subscribe to this entry?

 [2019-08-07 13:03 UTC] jeremy at derusse dot com
Casting an instance of \Datetime to an array should return date's properties. Since PHP 7.4, the array is empty.


Test script:
var_dump((array) new \Datetime('2000-01-01'));

Expected result:
array(3) {
  string(26) "2000-01-01 00:00:00.000000"
  string(16) "Europe/Amsterdam"

Actual result:
array(0) {


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-08-07 16:02 UTC]
Casting to an array is undefined, see here:
("The behaviour of an automatic conversion to array is currently undefined")
 [2019-08-07 16:21 UTC]
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2019-08-07 16:21 UTC]
> Casting to an array is undefined, […]

Not wrt. objects[1]:

| An array converts to an object with properties named by keys and
| corresponding values.

Anyhow, this is a deliberate change[2]; DateTime objects are not
supposed to have public properties.

[1] <>
[2] <>
 [2019-08-07 16:25 UTC]
We could easily restore the previous behavior wrt array casts here -- we're already keeping compatibility with json_encode() for example.

What's the use case?
 [2019-08-07 16:30 UTC]
-Status: Not a bug +Status: Feedback -Assigned To: cmb +Assigned To: nikic
 [2019-08-07 16:30 UTC]
Well, then ... :)
 [2019-08-07 20:41 UTC] jeremy at derusse dot com
-Status: Feedback +Status: Assigned
 [2019-08-07 20:41 UTC] jeremy at derusse dot com
We (symfony) implemented a generic serialization mechanism that uses array casts to extract state from objects. 
DateTime was not needing any special care, but now it would because the behavior changed between php 7.3 and 7.4, IMHO, it's a BC break.

Not another change in the bevahior of \Datetime casting.
 [2019-08-08 07:53 UTC]
-Summary: Casting a \Datetime to array no longer returns it properties +Summary: Casting a DateTime to array no longer returns its properties
 [2019-08-08 07:56 UTC]
Automatic comment on behalf of
Log: Fixed bug #78383
 [2019-08-08 07:56 UTC]
-Status: Assigned +Status: Closed
 [2019-08-08 08:23 UTC] jeremy at derusse dot com
Thank you @nikic
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Thu Dec 01 18:05:54 2022 UTC