How to add minute, minutes and sub minutes in Laravel application using Laravel Carbon? Well, this tutorial will show you how to add minutes with Laravel Carbon in Laravel application.
Carbon is a PHP package developed by Brian Nesbit which prolongs PHP’s DateTime class.
It offers some of the excellent functionality to handle with dates in PHP; It primarily deals with date timezones.
You will learn to add minutes on a current date with the help of Carbon package in Laravel, and this tutorial will demonstrate the Carbon in Laravel and specifically show you how to use addMinute(), addMinutes(), subMinutes() methods using Carbon date object.
Let’s begin understanding the Laravel Carbon example.
Getting Started
Laravel application can be installed with the following composer command:
composer create-project laravel/laravel --prefer-dist laravel-carbon-example
Move inside the project:
composer create-project laravel/laravel --prefer-dist laravel-carbon-example
Start laravel development server using below command:
php artisan serve
Open the app on below link:
http://127.0.0.1:8000
Thereafter, using below command generate a new controller for adding minute, minutes and sub minutes.
php artisan make:controller CalendarController
Define Minute with Laravel Carbon
Ideally, Inside the controller, use CarbonCarbon afterwards within the controller define index method to evoke the view. The addMinute() method is configured using Carbon. Consequently, we are adding minutes, and you can see the output in the browser.
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class CalendarController extends Controller
{
public function index()
{
return view('welcome');
}
public function addMinute()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->addMinute();
print(""
.print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
}
Define Minutes with Laravel Carbon
Likewise, you can pass minutes in addMinutes(7) method to add minutes using Carbon.
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class CalendarController extends Controller
{
public function index()
{
return view('welcome');
}
public function addMinutes()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->addMinutes(6);
print(""
.print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
}
Define Sub Minutes with Carbon
Same way you can use the subMinute() and subMinutes() methods.
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class CalendarController extends Controller
{
public function index()
{
return view('welcome');
}
public function addSubMinute()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->subMinutes(7);
print(""
.print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
}
Summary
The Carbon package powers the date-time in this tutorial you have seen how to easily add minutes and sub minutes using PHP’s very own Carbon package within the Laravel application.