There is a current date, there is some date that is taken from the base. I need to find the number of days between them, but when I try to do this, I get an error

Warning: date_diff() expects parameter 1 to be DateTimeInterface, string given 

Actually, the code itself

 $currdate = date('Ym-d', $_SERVER['REQUEST_TIME']);//текущая дата $userdate = date('Ym-d', $user['expire_date']); //дата из базы $difference = date_diff($currdate, $userdate); 

When I try to google, it gives out about the same code as mine.

  • you need not a function, but an object new DateTime. The function returns a string, and a new DateTime object. If it does not make a decision (you need not a function, but a new DateTime object), then I don’t know. - Sergey

1 answer 1

 $currdate = new DateTime(date('Ym-d', $_SERVER['REQUEST_TIME'])); $userdate = new DateTime(date('Ym-d', $user['expire_date'])); $difference = date_diff($currdate, $userdate); 

If you need a more accurate comparison, then Ymd changed to Ymd H:i:s

  • Thanks, it helped. And do not tell me, I have 17,714 days as the difference. Although it should output 40. What could be the problem? - Ingwar
  • @ IgorKulikov show $_SERVER['REQUEST_TIME'] and $user['expire_date'] - Manitikyl
  • here is $_SERVER['REQUEST_TIME'] = 2018-07-02, $user['expire_date'] = 2018-08-11 00:00:00 , but I still format it under Ymd - Ingwar