php类DateInterval的对象无法转换为字符串 [英] php Object of class DateInterval could not be converted to string

查看:480
本文介绍了php类DateInterval的对象无法转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用date_diff和date_create来获得两个已经转换为字符串的日期的区别。

i've tried using date_diff and date_create to get a difference from two date that's already converted to string.

这里是代码:

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

我收到此错误:

Object of class DateInterval could not be converted to string


推荐答案

您需要致电 DateInterval :: format() 以字符串形式显示该差异。

You need to call DateInterval::format() to display that difference as a string.

echo $diff->format('%d days');

有关所有可用的格式选项,请参阅手册。

See the manual for all of the available formatting options.

这篇关于php类DateInterval的对象无法转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆