[英]Fatal error when trying to format output using date_diff()
I'm using PHP 5.3.6 and when I try to run the code bellow I get the following error: " Fatal error: Call to a member function format() on a non-object in ...". 我使用的是PHP 5.3.6,当我尝试运行下面的代码时,出现以下错误:“致命错误:在...中的非对象上调用成员函数format()。”。
function diferenta_date($data_inceput, $data_sfarsit){
$interval = date_diff(date_create($data_inceput), date_create($data_sfarsit));
$output = $interval->format("Years:%Y,Months:%M,Days:%d,Hours:%H,Minutes:%i,Seconds:%s");
$return_output = array();
array_walk(explode(',', $output), function($val, $key) use(&$return_output) {
$v = explode(':', $val);
$return_output[$v[0]] = $v[1];
});
return $return_output;
}
What's wrong? 怎么了?
You need to check the return values. 您需要检查返回值。 The documentation says date_diff() returns: 文档说 date_diff()返回:
The DateInterval object representing the difference between the two dates or FALSE on failure . DateInterval对象,表示两个日期之间的差值; 如果失败 ,则为FALSE 。
date_diff() is failing and you are trying to use FALSE as an object. date_diff()失败,并且您尝试使用FALSE作为对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.