简体   繁体   中英

Php - How to sum hours?

I have to hours, 11:00:00 and 06:30:00 and I want to sum this 2 values, 17:30:00 .

I try to do that with carbon but it returns me a wrong value!

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

But it returns me 05:30 , the difference between this 2 hours.

Thank you

$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'); <--- This would likely cause you an issue.

Try instead

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

This code will do trick for you.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM