php date_diff() 期望参数 1 是 DateTimeInterface,给定的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24608529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:23:26  来源:igfitidea点击:

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

phpdatediff

提问by Shele?n Alag

i got this problem and i don't know what to do... they have thesame format

我遇到了这个问题,我不知道该怎么办...它们具有相同的格式

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

回答by Rikesh

Because you are passing string whereas date_diffexpects datetimeobject,

因为您正在传递字符串而date_diff需要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");

DEMO.

演示