Why can't I access DateTime-> in a PHP DateTime class? This is mistake?

Using the DateTime class if I try to run the following code:

 $mydate = new DateTime(); echo $mydate->date; 

I will return this error message

Note. Undefined property: DateTime :: $ date ...

This does not make sense, because when var_dump() run for the variable $mydate this clearly shows that this property exists and is publicly available:

 var_dump($mydate); object(DateTime)[1] public 'date' => string '2012-12-29 17:19:25' (length=19) public 'timezone_type' => int 3 public 'timezone' => string 'UTC' (length=3) 

Is this a bug in PHP or am I doing something wrong? I am using PHP 5.4.3.

+76
php datetime
Dec 29 '12 at 17:35
source share
5 answers

This is a known issue .

The date actually available is a side effect of var_dump() support here - derick@php.net

For some reason you shouldn't have access to the property, but var_dump shows it anyway. If you really want to get the date in this format, use the DateTime::format() function.

 echo $mydate->format('Ymd H:i:s'); 
+131
Dec 29
source share

Besides using DateTime::format() , you can access the property using reflection:

 <?php $dt = new DateTime(); $o = new ReflectionObject($dt); $p = $o->getProperty('date'); $date = $p->getValue($dt)); 

This is slightly faster than using format() , because format() formats a timestring that has already been formatted. Especially if you do it many times in a loop.

However, this is not regular PHP behavior. Bugreport is already registered as @Nile, mentioned in the comments above.

+14
Dec 29 '12 at 17:44
source share

As other answers noted, this is a problem with PHP that has not been resolved to date, but if it is a "side effect" of var_dump() , I'm not sure.

 echo ((array) new DateTime())['date']; // Works in PHP 7. 

I am sure that if the DateTime properties that were intended to be used by us would be available. But, like many inner classes, they are not, and you should not rely on "hacker" or "buggy" methods to fix the code. Instead, you should use their API .

 echo (new DateTime())->format('Ymd H:i:s'); 

If you are not satisfied, you can extend the class or use Carbon , which extends it for you.

 echo (new Carbon())->toDateTimeString(); 

If you know how var_dump() creates fake object output, see __debugInfo()

+1
Dec 22 '16 at 3:26
source share

If you just use var_Dump before setting the property date, everything works fine:

 $mydate = new DateTime(); var_Dump($mydate); echo '<br>'; echo $mydate->date; 

This provides:

 object(DateTime)#1 (3) { ["date"]=> string(26) "2017-04-11 08:44:54.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } 2017-04-11 08:44:54.000000 

So, you see that the date of the property exists even for the object. I do not understand this behavior. Just comment out on var_Dump and you will get an error again.

+1
Apr 11 '17 at 13:06 on
source share

The date DateTime property is protected.

You can display the date with the format function.

 <?php try { $time = new DateTime(); echo($time->format("Ymd H:i:s")); } catch (Exception $e) { } 

Or you can convert to an array:

 <?php try { $time = (array) new DateTime(); var_dump($time["date"]); } catch (Exception $e) { } 
0
Apr 10 '19 at 5:58
source share



All Articles