【问题标题】:Unable to get the date difference between two dates in php [duplicate]无法在php中获取两个日期之间的日期差异[重复]
【发布时间】:2018-07-31 13:50:58
【问题描述】:

所以我有以下代码,它在 SO 的其他地方找到了两个日期之间的差异。

 $datetime1 = new DateTime('2018-07-11');
 $datetime2 = new DateTime('2018-07-13');
 $diff = $datetime1->diff($datetime2);

我对那个没有问题,因为它运行良好,但是当我尝试自己应用它时,无法得到差异或差异为 0。

 $datetime1 = new DateTime($date_today);//format is 2018-07-01, string format
 $datetime2 = new DateTime($date);//format is 2018-07-31, string format
 $diff= $datetime1->diff($datetime2);

 log::info($diff->format('%d'));

这个输出是0,应该是30。你能帮我解决这个问题吗?

【问题讨论】:

  • 这个2018-07-0不是一个有效的日期可能是问题的一部分,或者只是一个不好的评论
  • 这是什么日期2018-07-0
  • 不,DateTime 将把 2018-07-0 更改为 2018-06-30
  • 在日历状态中,你有没有见过这个月的零号
  • @RiggsFolly 刚刚过了 2 月 31 日,PHP 也非常乐意将其延期到下个月

标签: php datediff date-difference


【解决方案1】:
log::info($diff->format('%a'));

解决办法

在 php 沙箱中查看此处:http://sandbox.onlinephpfunctions.com/code/32bcea357af442f92a1875e1669af6f01973df6d

$diff 是一个具有其他 DateInterval::format 参数的 DateInterval 对象

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 2017-03-27
    • 1970-01-01
    相关资源
    最近更新 更多