Support the ongoing development of Laravel.io →
posted 8 years ago
Views
Last updated 2 years ago.
0

Not tested, but I think it will work.

// set first time
$someTime = Carbon::createFromFormat('H:i:s', '18:15:10');
// add time
$someTime1 = $someTime->addHours(10)->addMinutes(15)->addSeconds(20)->toTimeString();
// get hours different
echo $someTime->diffInHours($someTime1);

There are other difference functions in the api also. For reference, http://carbon.nesbot.com/docs/#api-difference

Last updated 8 years ago.
0

I have tried this, but it is not what I need :D In my case there can be time only even if time is much more then 24h. So solutions here is simple :)

$dateTimeObject = new DateTime('20:15:13');

// let's add 10:05:25

$modifiedTimeString = $dateTimeObject->modify('+5 minutes')->modify('+25 seconds')->format('H:i:s);

// result here is 20:20:38

$stringExplode = explode(':', modifiedTimeString);

$realCountoOfHours = (int)$stringExplode[0]+10;

$goodTime = $realCountoOfHours . ':' . $stringExpldoe[1] . ':' . $stringExplode[2];

// result should be 30:20:38

Ofcourse every segment should be validated - if the resulting segment ( $stringExpldoe[x]) is less then 10 it should be converted padded with 0 to form nice time string like 02:05:03 and not like 2:5:3 If anyone knows a better way please share.

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

monas monas Joined 8 Mar 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.