Psyche
Psyche

Reputation: 8773

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?

Upvotes: 0

Views: 718

Answers (1)

Mark A. Hershberger
Mark A. Hershberger

Reputation: 676

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.

Upvotes: 1

Related Questions