为什么我不能在PHP的DateTime类中访问DateTime-> date?
如果我尝试运行以下代码,请使用DateTime
类:
Using the DateTime
class, if I try to run the following code:
$mydate = new DateTime();
echo $mydate->date;
我会找回此错误消息
通知:未定义的属性:DateTime :: $ date ...
Notice: Undefined property: DateTime::$date...
这没有意义,因为在变量$mydate
上运行var_dump()
时,它清楚地表明此属性存在并且可以公开访问:
Which doesn't make sense because when running var_dump()
on the variable $mydate
, it clearly shows that this property exists and is publicly accessible:
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)
这是PHP中的错误,还是我做错了什么?我正在使用PHP 5.4.3.
Is this a bug within PHP or am I doing something wrong? I'm using PHP 5.4.3.
这是已知问题
可用的日期实际上是对
var_dump()
的支持的副作用– derick@php.net
Date being available is actually a side-effect of support for
var_dump()
here – derick@php.net
由于某些原因,您不应该能够访问该属性,但是无论如何var_dump
都会显示该属性.如果您确实希望以这种格式获取日期,请使用 DateTime::format()
函数.
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');