Why can't I access DateTime->date in PHP's DateTime class?

前端 未结 5 1566
醉梦人生
醉梦人生 2020-11-22 06:44

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

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

I\'ll get back

5条回答
  •  一向
    一向 (楼主)
    2020-11-22 07:08

    If you just use a var_Dump before ask the property date everything works allright:

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

    This delivers:

    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 the property date exists even for the object. I can't understand this behaviour. Just comment out the var_Dump and you will get the error again.

提交回复
热议问题