|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-08-27 07:52 UTC] klawd at kamundo dot de
Description:
------------
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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 06:00:01 2025 UTC |
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::$dateOkay, 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.How I worked around the problem: ... $dt=new DateTime('2016-07-06 00:00:00'); $tmpObj = (array)($dt); // Only for circumvention, without this line $dt->date return null. $dtx = $dt->date; echo $dtx; // display: 2016-07-06 00:00:00 ...