【问题标题】:DateInterval - Difference between two timesDateInterval - 两次之间的差异
【发布时间】:2017-03-19 15:42:51
【问题描述】:

我有 2 次 (DateInterval - php),我想知道它们之间的区别 (类似于 DateTime 的“diff()”函数).

我的情况:

$task->estimate->format('%H:%I');          // HOURS:MINUTES / DateInterval 
$task->total_time->format('%H:%I');        // HOURS:MINUTES / DateInterval 
$task->total_time->diff($task->estimate);  // Not workin, only for DateTime
// difference between them?

不计入秒就太好了,一定有简单的方法来做到这一点。

【问题讨论】:

  • 问题是什么?
  • @niceman 不,date_diff 不起作用(date_diff() 期望参数 1 是 DateTimeInterface,给定对象)并且 date_create() 也不起作用(不接受 DateInterval 类型的正常或格式化日期)
  • @JaredFarrish 如何获取“DataInterval”类型的两个值之间的差异(不是 DateTime)
  • 如果您可以将估计和总时间转换为总分钟数(或秒...),您可以减去两个总秒数,然后将其转换为分钟数和/或小时显示。

标签: php dateinterval


【解决方案1】:

可以计算(任意时间+区间1)和(同一时间+区间2)的diif

$estimate = new DateInterval('PT6H8M');
$total_time  = new DateInterval('PT5H7M');
$time = new DateTime('midnight');
$time->add($total_time);
$time1 = new DateTime('midnight');
$diff = $time1->add($estimate)->diff($time);
var_dump($diff); // object(DateInterval) ... ["h"]=> int(1) ["i"]=> int(1)  ...

【讨论】:

    猜你喜欢
    • 2019-04-14
    • 2015-02-22
    • 2012-08-16
    • 1970-01-01
    • 2022-11-07
    • 2013-09-25
    • 2011-06-01
    • 2016-04-01
    • 1970-01-01
    相关资源
    最近更新 更多