从数组对象获取值(DateTime)

时间:2014-11-05 20:45:55

标签: php date

我尝试使用$ class-> diff_time('2014-02-11',date(“Ymd”))[0] ['date']访问该值,但似乎无法将对象转换为阵列。甚至这个表单函数()[表]是显示数据的合法方法吗?看起来很不自然。

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
)

1 个答案:

答案 0 :(得分:1)

似乎日期是对象的字段,因此请尝试

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