drq231358 2012-12-29 17:35
浏览 109
已采纳

为什么我不能在PHP的DateTime类中访问DateTime-> date? 这是一个错误吗?

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

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

I'll get back this error message

Notice: Undefined property: DateTime::$date...

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)

Is this a bug within PHP or am I doing something wrong? I'm using PHP 5.4.3.

  • 写回答

5条回答 默认 最新

  • douqiang5933 2012-12-29 17:42
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?