In today’s article we will illustrate how to use the Carbon library to add and subtract from a given time in Laravel. As you know already, Laravel is capable of doing pretty much anything.
This is tested with Laravel7, 8,9 versions.
Here you need to take note of Carbon – Well known PHP API for working with DateTime, to do the operations on DateTime. Furthermore, for adding hours to current time Carbon provides a method called addHour(), and for subtracting hours from current time Carbon provides subHour().
use Carbon\Carbon;
Add an hour to the current time
//This will add 1 hour from now $dateTimeNew = Carbon::now()->addHour(); print_r($dateTimeNow->toArray()); print_r($dateTimeNew->toArray());
Output will be like the below
Array ( [year] => 2022 [month] => 6 [day] => 10 [dayOfWeek] => 5 [dayOfYear] => 161 [hour] => 8 [minute] => 34 [second] => 8 [micro] => 295345 [timestamp] => 1654850048 [formatted] => 2022-06-10 08:34:08 [timezone] => Carbon\CarbonTimeZone Object ( [timezone_type] => 3 [timezone] => UTC ) ) Array ( [year] => 2022 [month] => 6 [day] => 10 [dayOfWeek] => 5 [dayOfYear] => 161 [hour] => 9 [minute] => 34 [second] => 8 [micro] => 295629 [timestamp] => 1654853648 [formatted] => 2022-06-10 09:34:08 [timezone] => Carbon\CarbonTimeZone Object ( [timezone_type] => 3 [timezone] => UTC ) )
Add hours to current time
$dateTimeNow = Carbon::now(); //This will add 5 hours from now $dateTimeNew = Carbon::now()->addHour(4); print_r($dateTimeNow->toArray()); print_r($dateTimeNew->toArray());
The output will be like the below
Array ( [year] => 2022 [month] => 6 [day] => 10 [dayOfWeek] => 5 [dayOfYear] => 161 [hour] => 8 [minute] => 36 [second] => 46 [micro] => 190319 [timestamp] => 1654850206 [formatted] => 2022-06-10 08:36:46 [timezone] => Carbon\CarbonTimeZone Object ( [timezone_type] => 3 [timezone] => UTC ) ) Array ( [year] => 2022 [month] => 6 [day] => 10 [dayOfWeek] => 5 [dayOfYear] => 161 [hour] => 12 [minute] => 36 [second] => 46 [micro] => 190531 [timestamp] => 1654864606 [formatted] => 2022-06-10 12:36:46 [timezone] => Carbon\CarbonTimeZone Object ( [timezone_type] => 3 [timezone] => UTC ) )
Subtract an hour from the current time
$dateTimeNow = Carbon::now(); //This will subtract 1 hour from now $dateTimeNew = Carbon::now()->subHour(); print_r($dateTimeNow->toArray()); print_r($dateTimeNew->toArray());
The output will be like the below
Array ( [year] => 2022 [month] => 6 [day] => 10 [dayOfWeek] => 5 [dayOfYear] => 161 [hour] => 8 [minute] => 38 [second] => 52 [micro] => 554319 [timestamp] => 1654850332 [formatted] => 2022-06-10 08:38:52 [timezone] => Carbon\CarbonTimeZone Object ( [timezone_type] => 3 [timezone] => UTC ) ) Array ( [year] => 2022 [month] => 6 [day] => 10 [dayOfWeek] => 5 [dayOfYear] => 161 [hour] => 7 [minute] => 38 [second] => 52 [micro] => 554528 [timestamp] => 1654846732 [formatted] => 2022-06-10 07:38:52 [timezone] => Carbon\CarbonTimeZone Object ( [timezone_type] => 3 [timezone] => UTC ) )
Substract many hours from current time
$dateTimeNow = Carbon::now(); //This will subtract 5 hours from now $dateTimeNew = Carbon::now()->subHour(5); print_r($dateTimeNow->toArray()); print_r($dateTimeNew->toArray());
The output will be like the below
Array ( [year] => 2022 [month] => 6 [day] => 10 [dayOfWeek] => 5 [dayOfYear] => 161 [hour] => 8 [minute] => 39 [second] => 24 [micro] => 726097 [timestamp] => 1654850364 [formatted] => 2022-06-10 08:39:24 [timezone] => Carbon\CarbonTimeZone Object ( [timezone_type] => 3 [timezone] => UTC ) ) Array ( [year] => 2022 [month] => 6 [day] => 10 [dayOfWeek] => 5 [dayOfYear] => 161 [hour] => 3 [minute] => 39 [second] => 24 [micro] => 726309 [timestamp] => 1654832364 [formatted] => 2022-06-10 03:39:24 [timezone] => Carbon\CarbonTimeZone Object ( [timezone_type] => 3 [timezone] => UTC ) )
Leave a Reply