为什么我不能在 PHP 的 DateTime 类中访问 DateTime->date?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14084222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:35:54  来源:igfitidea点击:

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

phpdatetime

提问by okey_on

Using the DateTimeclass, if I try to run the following code:

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

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

I'll get back this error message

我会回复这个错误信息

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

注意:未定义的属性: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,它清楚地表明此属性存在并且可公开访问:

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.

这是 PHP 中的错误还是我做错了什么?我正在使用 PHP 5.4.3。

回答by jeremy

This is a known issue.

这是一个已知问题

Date being available is actually a side-effect of support for var_dump()here – [email protected]

可用日期实际上是var_dump()此处支持的副作用– [email protected]

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

出于某种原因,您不应该能够访问该属性,但var_dump无论如何都要显示它。如果您真的想以该格式获取日期,请使用该DateTime::format()函数。

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

回答by hek2mgl

Besides calling DateTime::format()you can access the property using reflection:

除了调用之外,DateTime::format()您还可以使用反射访问该属性:

<?php

$dt = new DateTime();
$o = new ReflectionObject($dt);
$p = $o->getProperty('date');
$date = $p->getValue($dt);

This is slight faster than using format()because format()formats a timestring that has already been formatted. Especially if you do it many times in a loop.

这比使用略快,format()因为format()格式化已经格式化的时间字符串。特别是如果你在循环中多次这样做。

However this is not a regular behaviour of PHP. A bugreport has already been filed as @Nile mentioned in the comments above.

然而,这不是 PHP 的常规行为。上面评论中提到的@Nile 已经提交了一个错误报告。

回答by Xorifelse

As noted by the other answers, it is an issue with PHP which is unresolved as of today but if it is a 'side effect' of var_dump()I am not so sure..

正如其他答案所指出的,这是 PHP 的一个问题,截至今天尚未解决,但如果它是var_dump()我不太确定的“副作用” 。

echo ((array) new DateTime())['date']; // Works in PHP 7.

What I am sure about is that ifthe properties of DateTimewhere meant to be used by us it would have been made available. But like many internal classes they are notand you shouldn't rely on "hacky" or "glitchy" methods to fix your code. Instead you should use their API.

我确信的是,如果DateTime我们打算使用的where属性将可用。但与许多内部类一样,它们不是,您不应该依赖“hacky”或“glitchy”方法来修复您的代码。相反,您应该使用他们的API

echo (new DateTime())->format('Y-m-d H:i:s');

If you are not satisfied you can extend the class or perhaps use Carbonthat extends it for you.

如果您不满意,您可以扩展该类,或者使用为您扩展它的Carbon

echo (new Carbon())->toDateTimeString();

If you wounder how var_dump()creates a fake output of an object take a look at __debugInfo()

如果您更清楚如何var_dump()创建对象的假输出,请查看__debugInfo()

回答by filipe

The date property of DateTime is protected.

DateTime 的日期属性受到保护。

You can display the date with format function.

您可以使用格式功能显示日期。

<?php

try {
    $time = new DateTime();
    echo($time->format("Y-m-d H:i:s"));
} catch (Exception $e) {
}

Or you can convert to array:

或者您可以转换为数组:

<?php

try {
    $time = (array) new DateTime();
    var_dump($time["date"]);
} catch (Exception $e) {
}

回答by Sascha

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

如果您只是在询问属性日期之前使用var_Dump一切正常:

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

因此,您会看到即使对象的属性日期也存在。我无法理解这种行为。只需注释掉 var_Dump,您就会再次收到错误消息。