简体   繁体   中英

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 ...".

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:

The DateInterval object representing the difference between the two dates or FALSE on failure .

date_diff() is failing and you are trying to use FALSE as an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM