为什么在PHP的DateTime类中无法访问DateTime-> date?这是一个bug吗? [英] Why can't I access DateTime->date in PHP's DateTime class? Is it a bug?

查看:148
本文介绍了为什么在PHP的DateTime类中无法访问DateTime-> date?这是一个bug吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 DateTime 类,如果我尝试运行以下代码。

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

$mydate = new DateTime();
echo $mydate->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() here - 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');

这篇关于为什么在PHP的DateTime类中无法访问DateTime-> date?这是一个bug吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆