2013-07-24 29 views
-1
$now = new DateTime(); 
print_r($now); 
print $now->date; // print the current date 

但是,如果print_r($ now);是評論它顯示錯誤?PHP DateTime()發行日期

$now = new DateTime(); 
print $now->date; // Notice: Undefined property: DateTime::$date in 
+1

沒有'日期'定義在成員函數/屬性[DateTime](http://php.net/manual/en/class.datetime.php) – DevZer0

+0

請不要使用未記錄的屬性或不好的事情會發生。 – PeeHaa

回答

0

這是一個bug in PHP(我不知道到底受到影響的版本)。

DateTimedate屬性,但要求它print_r創建了一個「隱藏」屬性,它看起來像它的存在(它顯示與print_r),但在現實中是不是(你不能得到它的值) 。

而不是這個,使用DateTime::format以任何你想要的格式獲取日期值。

+0

@downvoter:你自己測試過嗎?因爲我知道一個事實,我是正確的。 – Jon

+0

嗯,這是正確的...但它也是一個愚蠢我相信:) –

+0

@傑克:雅,我發現了愚蠢和錯誤,CVed和鏈接。但是sheesh。 – Jon