2014-11-05 166 views
-1

我試圖訪問$ class-> diff_time('2014-02-11',date(「Ymd」))[0] ['date']的值,但它似乎它不可能轉換數組中的對象。甚至這種形式的函數()[table]是顯示數據的合法方法嗎?看起來很不自然。從數組對象獲取值(DateTime)

function diff_time($lowest,$highest){ 
    /* input format @$lowest @$highest 
       Y-m-d 
    */ 
    $container = array(); 
    $dStart = new DateTime($lowest); 
    $dEnd = new DateTime($highest); 
    $dDiff = $dStart->diff($dEnd); 
    $containser = array($dStart,$dEnd,$dDiff->days,$dDiff->format('%R')); 
    return $containser; 
} 
var_dump($class->diff_time('2014-02-11',date("Y-m-d"))); 

array(4) { 
    [0]=> 
    object(DateTime)#3 (3) { 
    ["date"]=> 
    string(19) "2014-02-11 00:00:00" 
    ["timezone_type"]=> 
    int(3) 
    ["timezone"]=> 
    string(13) "Europe/Berlin" 
    } 
    [1]=> 
    object(DateTime)#4 (3) { 
    ["date"]=> 
    string(19) "2014-11-05 00:00:00" 
    ["timezone_type"]=> 
    int(3) 
    ["timezone"]=> 
    string(13) "Europe/Berlin" 
    } 
    [2]=> 
    int(267) 
    [3]=> 
    string(1) "+" 
} 

print_r($class->diff_time('2014-02-11',date("Y-m-d"))[0]); 

DateTime Object 
(
    [date] => 2014-02-11 00:00:00 
    [timezone_type] => 3 
    [timezone] => Europe/Berlin 
) 
+0

什麼樣的價值是你想獲得? – 2014-11-05 20:53:43

+0

@JohnConde [0] [「date」]和[1] [「date]因爲[2]和[3]很容易得到:) – Kavvson 2014-11-05 20:54:22

+0

我還不知道你想要什麼價值 – 2014-11-05 20:54:49

回答

1

看來日期是對象的字段,所以儘量

$class->diff_time('2014-02-11',date("Y-m-d"))[0]->date; 
+0

注意:未定義的屬性:DateTime :: $ date for line $ class-> diff_time('2014-02-11',date(「Ymd」))[0] - > date – Kavvson 2014-11-05 20:56:54

+0

請問,輸出是什麼'print_r($ class-> diff_time('2014-02-11',date(「Ymd」))[0]);'? – mgaido 2014-11-05 20:59:37

+0

添加到問題 – Kavvson 2014-11-05 21:01:17