Php - 如何累计小时数?

时间:2018-01-29 12:17:06

标签: php

我有几个小时,11:00:0006:30:00,我想总结这两个值,17:30:00

我尝试用carbon做到这一点,但它给我一个错误的值!

$startTime1 = Carbon::createFromFormat('H:i:s', '11:00:00');
$startTime2 = Carbon::createFromFormat('H:i:s', '06:30:00');

$total_in_seconds = $startTime1->timestamp + $startTime2->timestamp;
$totalInterventions = Carbon::createFromTimestamp($total_in_seconds)->format('h:i:s');

但它返回05:30,这是2小时之间的差异。

谢谢

2 个答案:

答案 0 :(得分:2)

$startTime1 = Carbon::createFromFormat('H:i:s', '11:00:00');
$startTime2 = Carbon::createFromFormat('H:i:s', '06:30:00');

$total_in_seconds = $startTime1->timestamp + $startTime2->timestamp;
$totalInterventions = Carbon::createFromTimestamp($total_in_seconds)-

>format('h:i:s');< ---这可能会给您带来麻烦。

尝试改为

>format('H:i:s');

答案 1 :(得分:1)

这段代码对你来说很有用。

        $time = new Carbon($startTime1);
        $startTime2=new Carbon($startTime2);
        $time->diffForHumans($startTime2);