【问题标题】:php Object of class DateInterval could not be converted to stringDateInterval 类的 php 对象无法转换为字符串
【发布时间】:2015-02-20 03:42:18
【问题描述】:

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

代码如下:

$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

【问题讨论】:

    标签: php datetime date-math dateinterval


    【解决方案1】:

    您需要调用 DateInterval::format() 以将差异显示为字符串。

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

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

    【讨论】:

    • 做到了..感谢您的参考和回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 2021-11-10
    • 2023-04-06
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多