Laravel has the Carbon
dependency attached to it. So, in order to perform basic to complex date and time calculations you can use this dependency. In this tutorial we will see some examples of addMinute
and addMinutes
methods of Carbon
class.
Along with many other useful methods there are two general methods to add minute to a given time. These two methods are addMinute
and addMinutes
.
Let’s get started with looking at the example to add minutes to current time in Laravel. Minute are added by using Carbon
. We will use another useful helper function now()
of Laravel. now()
returns a Carbon
object by default! This example applies to Laravel 6, 7, 8, 9 and 10 versions.
Add 1 minutes using addMinute() method
$now = now(); print_r($now->toArray()); print_r($now->addMinute()->toArray());
Sample Output:
Array ( [year] => 2023 [month] => 5 [day] => 27 [dayOfWeek] => 6 [dayOfYear] => 147 [hour] => 12 [minute] => 3 [second] => 46 [micro] => 820870 [timestamp] => 1685189026 [formatted] => 2023-05-27 12:03:46 [timezone] => Carbon\CarbonTimeZone Object ( [timezone_type] => 3 [timezone] => UTC ) ) Array ( [year] => 2023 [month] => 5 [day] => 27 [dayOfWeek] => 6 [dayOfYear] => 147 [hour] => 12 [minute] => 4 [second] => 46 [micro] => 820870 [timestamp] => 1685189086 [formatted] => 2023-05-27 12:04:46 [timezone] => Carbon\CarbonTimeZone Object ( [timezone_type] => 3 [timezone] => UTC ) )
Add 10 minutes using addMinutes() method
$now = now(); print_r($now->toArray()); print_r($now->addMinutes(10)->toArray());
Subtract 1 minute using subMinute()
$now = now(); print_r($now->toArray()); print_r($now->subMinute()->toArray());
Subtract 1o minute using subMinutes()
$now = now(); print_r($now->toArray()); print_r($now->subMinutes(10)->toArray());
Along with addSeconds, addHour, addHours, addDay, addDays, addMonth, addMonths and various other calculation methods there are some other general methods in Carbon to get particular date and time. For example, today(), tomorrow(), yesterday() methods.
(I hope this tutorial helped you to learn something new about Laravel and Carbon today. Please share this post to help others learn and grow. Thanks!)