PASSWORD RESET

Your destination for complete Tech news

How to add hours in a time in Laravel Carbon?

616 0
< 1 min read

In Laravel, Carbon is a popular date/time library that provides an easy way to work with dates and times in PHP. To add hours to a time using Carbon in Laravel, you can use the addHours() method.

Here’s an example code snippet:

use Carbon\Carbon;

$time = Carbon::parse('13:30');

$updatedTime = $time->addHours(2);

echo $updatedTime->toTimeString(); // Output: 15:30:00

In this example, we’re starting with a time string of ’13:30′, which is parsed into a Carbon object using the static parse() method. This creates a new Carbon instance representing the given time.

The addHours() method is then called on the Carbon object, which adds 2 hours to the time and returns a new Carbon instance representing the resulting time.

Finally, the toTimeString() method is called on the resulting Carbon instance to output the updated time in the format of “HH:MM:SS”.

The $updatedTime variable is then output using the echo statement, which will output the updated time, in this case “15:30:00”.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.