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

前端 未结 5 1569
醉梦人生
醉梦人生 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:15

    This is a known issue.

    Date being available is actually a side-effect of support for var_dump() here – derick@php.net

    For some reason, you're not supposed to be able to access the property but var_dump shows it anyways. If you really want to get the date in that format, use the DateTime::format() function.

    echo $mydate->format('Y-m-d H:i:s');
    

提交回复
热议问题