php 类DateInterval的php对象无法转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28621270/
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
php Object of class DateInterval could not be converted to string
提问by Christian Burgos
i've tried using date_diff and date_create to get a difference from two date that's already converted to string.
我试过使用 date_diff 和 date_create 从两个已经转换为字符串的日期中获取差异。
here's the code:
这是代码:
$date_1 = date_create();
$date_now = date_format($date_1, 'Y-m-d');
//echo $date_now . "\n";
$date=date_create($date_now);
date_add($date,date_interval_create_from_date_string("3 days"));
$date_return = date_format($date,"Y-m-d");
$diff = date_diff(date_create($date_now), date_create($date_return));
echo $diff;
and i am getting this error:
我收到此错误:
Object of class DateInterval could not be converted to string
回答by John Conde
You need to call DateInterval::format()
to display that difference as a string.
您需要调用DateInterval::format()
以将该差异显示为字符串。
echo $diff->format('%d days');
See the manual for all of the available formatting options.
有关所有可用的格式选项,请参阅手册。