0

I'm having a problem adding two(2) timestamp for example:

00:30:00
00:45:31
========
01:15:31 

I can't figure it out how to do it using laravel with carbon...

Kenny Horna
  • 13,485
  • 4
  • 44
  • 71
  • These aren't timestamps, and Carbon can't handle time alone - it's for working with `DateTime` objects. It should be fairly trivial to `explode()` by `:` on both of your times and do the math. – ceejayoz Aug 22 '19 at 01:32
  • Take a look at this: https://stackoverflow.com/questions/37227075/laravel-carbon-adding-two-timestamps-together – N. Djokic Aug 22 '19 at 01:32
  • Carbon can using CarbonInterval: `CarbonInterval::minutes(30)->seconds(0)->add(CarbonInterval::minutes(45)->seconds(31))->cascade()->format('%h:%i:%s')` – KyleK Aug 31 '19 at 19:03

4 Answers4

4

You can't add time like they are integer. One of the way is to convert it to another format and then add it. After adding them, convert it back to time type.

For example, try this:

$time1 = "00:30:00";
$time2 = "00:45:31";

$secs = strtotime($time2) - strtotime("00:00:00");
$result = date("H:i:s", strtotime($time1) + $secs);

dd($result); // "01:15:31"
Kenny Horna
  • 13,485
  • 4
  • 44
  • 71
Nabil Farhan
  • 1,444
  • 3
  • 25
  • 41
0

My practice is to make TimeHelper class where I declare all my static methods that are related with calculations of time. So basically I would make static function like this in TimeHelper class:

public static function addTwoTimes($time1 = "00:00:00", $time2 = "00:00:00"){
        $time2_arr = [];
        $time1 = $time1;
        $time2_arr = explode(":", $time2);
        //Hour
        if(isset($time2_arr[0]) && $time2_arr[0] != ""){
            $time1 = $time1." +".$time2_arr[0]." hours";
            $time1 = date("H:i:s", strtotime($time1));
        }
        //Minutes
        if(isset($time2_arr[1]) && $time2_arr[1] != ""){
            $time1 = $time1." +".$time2_arr[1]." minutes";
            $time1 = date("H:i:s", strtotime($time1));
        }
        //Seconds
        if(isset($time2_arr[2]) && $time2_arr[2] != ""){
            $time1 = $time1." +".$time2_arr[2]." seconds";
            $time1 = date("H:i:s", strtotime($time1));
        }

        return date("H:i:s", strtotime($time1));
    } 

So wherever you need to sum two times just call TimeHelper::addTwoTimes(arg1, arg2);

I took example function from user Cha from this topic: PHP add up two time variables

N. Djokic
  • 898
  • 1
  • 6
  • 19
0

This how you can achieve adding two times with a Carbon date. The explanation is shown in inline comments

$time1 = '00:30:00';
$time2 = '00:45:31';

// Use the first time to crate a new Carbon date
$baseDate = Carbon::parse($time1);

// Deconstruct the second time into hours, minutes and seconds
list($addHour, $addMinutes, $addSeconds) = explode(':', $time2);

// Add the hours, minutes and seconds to the Carbon object
$baseDate->addHours($addHour)->addMinutes($addMinutes)->addSeconds($addSeconds);

// Print the result: 00:30:00 + 00:45:31 = 01:15:31
echo "{$time1} + {$time2} = " .  $baseDate->toTimeString();
Pablo
  • 5,897
  • 7
  • 34
  • 51
0

You can use CarbonInterval class from Carbon library:

$durations = [
    '00:30:00',
    '00:45:31',
];

function getDuration($string)
{
    [$hours, $minutes, $seconds] = explode(':', $string);

    return CarbonInterval::hours($hours)->minutes($minutes)->seconds($seconds);
}

$total = getDuration(array_shift($durations));

foreach ($durations as $duration) {
    $total->add(getDuration($duration));
}

echo $total->cascade()->format('%h:%i:%s'); // 1:15:31
KyleK
  • 4,643
  • 17
  • 33