0

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?

Psyche
  • 8,513
  • 20
  • 70
  • 85
  • 3
    You need to learn how to [READ and debug](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) error messages. Everything you needed to solve the problem was in the error message. $interval is not a valid object – Anigel Jul 29 '13 at 13:50
  • Seems like date_diff is not returning you an object, but I can not guess why, sorry. – m4t1t0 Jul 29 '13 at 13:50
  • `date_diff()` probably returned `false` because it failed, probably because one (or both) of the `date_create()` calls failed. What are the values of `$data_inceput` and `$data_sfarsit`? – Wiseguy Jul 29 '13 at 13:51
  • What type of `$data_inceput`, `$data_sfarsit` vars? – Bora Jul 29 '13 at 13:52
  • @Bora, string values, like '2013-07-28'. – Psyche Jul 29 '13 at 13:53
  • I didnt get any error with string params like '2013-07-28'. PHP 5.3.13 – Bora Jul 29 '13 at 14:02

1 Answers1

1

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.