|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49382 can't access DateTime->date
Submitted: 2009-08-27 07:52 UTC Modified: 2010-11-18 12:29 UTC
Avg. Score:4.0 ± 0.9
Reproduced:61 of 62 (98.4%)
Same Version:34 (55.7%)
Same OS:17 (27.9%)
From: klawd at kamundo dot de Assigned: derick
Status: Assigned Package: Date/time related
PHP Version: 5.3.0 OS: Debian GNU/Linux 5.0
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2009-08-27 07:52 UTC] klawd at kamundo dot de
Can not access property date of DateTime.

Reproduce code:
$dt=new DateTime('1742-05-23 00:00:00'); echo $dt->date;
gets me Notice: Undefined property: DateTime::$date

strangely though, this works:
$dt=new DateTime('1742-05-23 00:00:00'); print_r($dt); echo $dt->date;
DateTime Object ( [date] => 1742-05-23 00:00:00 [timezone_type] => 3 [timezone] => UTC ) 1742-05-23 00:00:00


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-07 20:22 UTC]
-Package: Date/time related +Package: Feature/Change Request
 [2010-03-07 20:22 UTC]
->date being available is actually a side-effect of support for var_dump() here. I'll mark this as a feature request as it was not intended to work.
 [2010-08-04 22:36 UTC] weirdan at gmail dot com
if this was not intended to work this way, why won't you just fix it so that no 
properties are created as a result of print_r / Reflection::export() / foreach() / 
property_exist / array cast?
 [2010-11-18 12:29 UTC]
-Package: Feature/Change Request +Package: Date/time related
 [2012-12-29 17:56 UTC] info at metashock dot net
I think it is interesting that the property can be accessed using reflection. Using the following example I can access the property:

$dt = new DateTime();
$o = new ReflectionObject($dt);
$p = $o->getProperty('date');
$date = $p->getValue($dt)); // returns the value of DateTime::$date
 [2013-07-09 11:29 UTC] hanskrentel at yahoo dot de
I wonder how this qualifies as a bug as any object can have variable properties 
and accessing undefined ones give notices.

Just sounds like standard PHP behavior here.
 [2013-07-09 11:35 UTC] weirdan at gmail dot com
I'd say the bug is not that the property is not present before var_dump'ing 
object, but that the property is suddenly created. Unexpected side-effect.
 [2014-12-08 17:48 UTC] phpbug at sirdiego dot de
Okay, so I don't know if this is new or relevant in any case, but I found something similar to var_dump.

php > $object = new \DateTime();
php > echo $object->date . PHP_EOL;
PHP Notice:  Undefined property: DateTime::$date in php shell code on line 1
PHP Stack trace:
PHP   1. {main}() php shell code:0

php > get_object_vars($object);
php > echo $object->date . PHP_EOL;
2014-12-08 18:44:15

For me this also an unexpected side-effect and would say it is a bug.
PHP Copyright © 2001-2015 The PHP Group
All rights reserved.
Last updated: Fri Nov 27 00:01:30 2015 UTC