简体   繁体   English

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

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

Using the DateTime class, 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:这没有意义,因为在变量$mydate上运行var_dump()时,它清楚地表明此属性存在且可公开访问:

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?这是 PHP 中的错误还是我做错了什么? I'm using PHP 5.4.3.我正在使用 PHP 5.4.3。

This is a known issue .这是一个已知问题

Date being available is actually a side-effect of support for var_dump() here – derick@php.net可用日期实际上是此处支持var_dump()副作用 – derick@php.net

For some reason, you're not supposed to be able to access the property but var_dump shows it anyways.出于某种原因,您不应该能够访问该属性,但var_dump无论如何都会显示它。 If you really want to get the date in that format, use the DateTime::format() function.如果您确实想以该格式获取日期,请使用DateTime::format()函数。

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

Update: The behaviour has changed in PHP7.3, the original answer doesn't work anymore.更新:PHP7.3 中的行为已更改,原始答案不再有效。 To get the same results with all PHP versions, incl.要在所有 PHP 版本中获得相同的结果,包括。 >=7.3, you can use the following code: >=7.3,可以使用如下代码:

$dt = new DateTime();
$date = $dt->format('Y-m-d\TH:i:s.v');

For the record, the original answer:为了记录,原始答案:

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 slightly faster than using format() because format() formats a timestring that has already been formatted.这比使用format()稍快,因为format()格式化已经格式化的时间字符串。 Especially if you do it many times in a loop.特别是如果你在循环中多次这样做。

However this is not a documented behaviour of PHP, it may change at any time.然而,这不是 PHP 的记录行为,它可能随时更改。

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 if the properties of DateTime where meant to be used by us it would have been made available.我可以肯定的是,如果我们打算使用DateTime的属性,它就会可用。 But like many internal classes they are not and you shouldn't rely on "hacky" or "glitchy" methods to fix your code.但与许多内部类一样,它们不是,您不应该依赖“hacky”或“glitchy”方法来修复您的代码。 Instead you should use their API .相反,您应该使用他们的API

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

If you are not satisfied you can extend the class or perhaps use Carbon that 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()

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) {
}

If you just use a var_Dump before 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,您就会再次收到错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM