doulu8341 2015-03-21 20:53
浏览 37
已采纳

PHP DateTime对象的奇怪(?)行为

I am trying to run the following

$d = "2015-Mar-22";
$dsd = DateTime::createFromFormat('Y-M-d H:i:s', $d . " 00:00:00");
$ds = strtotime($dsd->date);

and get

PHP Notice:  Undefined property: DateTime::$date in php shell code on line 1

However, if I call var_dump($dsd) before converting to a unix timestamp I get the correct answer:

echo strtotime($dsd->date);

1427007600

Am I missing something here?

  • 写回答

1条回答 默认 最新

  • duanlujiaji10335 2015-03-21 21:35
    关注

    You're attempting to use an undocumented property that is only ever made available due to a side effect.

    ->date being available is actually a side-effect of support for var_dump()


    If you just want a timestamp use $dsd->getTimestamp() instead of strtotime($dsd->date).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?