Php date_diff()要求参数1为DateTimeInterface,字符串为给定值

Php date_diff()要求参数1为DateTimeInterface,字符串为给定值,php,datediff,Php,Datediff,它们的格式相同: $date_expire = '2014-08-06 00:00:00'; $date1 = date("Y-m-d G:i:s"); $date2 = date_create($date_expire); $diff = date_diff($date1, $date2); //this line makes error. 但我得到了这个错误: date_diff()要求参数1为DateTimeInterface,字符串为给定值 因为您正在传递字符串,

它们的格式相同:

$date_expire = '2014-08-06 00:00:00';
$date1 = date("Y-m-d G:i:s");
$date2 = date_create($date_expire);

$diff = date_diff($date1, $date2); //this line makes error.
但我得到了这个错误:

date_diff()要求参数1为DateTimeInterface,字符串为给定值


因为您正在传递字符串,而需要
datetime
对象

$date_expire = '2014-08-06 00:00:00';    
$date = new DateTime($date_expire);
$now = new DateTime();

echo $date->diff($now)->format("%d days, %h hours and %i minuts");