The primary topic of this tutorial is to add months in the Laravel 9 application using the Laravel Carbon package.
Throughout this tutorial, you will see the examples of Laravel carbon add the month, add months, add sub month, and add sub months, respectively, to the current date.
This tutorial created aiming laravel application although you can add months and sub months in older laravel versions such 7 and 6 by importing carbon class in laravel controller.
Carbon is a useful PHP package that helps empower the PHP DateTime class; carbon offers innumerable date-time methods to deal with date time.
After completing this tutorial, you will have the exact idea of dealing with Carbon date object and date object usage in the laravel app. Consequently, you will be able to add single and multiple months using addMonth() and addMonths() methods; also, you will learn to use subMonth() and subMonths() methods simultaneously through this tutorial.
Without further ado, let’s start.
Create Laravel Application
Start creating laravel application:
composer create-project laravel/laravel --prefer-dist laravel-carbon-months-example
Get into the project root:
cd laravel-carbon-months-example
Start the laravel application:
php artisan serve
Open below URL in browser:
http://127.0.0.1:8000
Next, generate a brand new controller and add months and sub months in Laravel:
php artisan make:controller DateTimeController
All the example of code will be added to app/Http/Controllers/DateTimeController.php file:
Define Month with addMonth()
Import Carbon on top, define addMonth() function, define the current date with now() method, and add the month to the current date. Not just that, print the result and check on the browser.
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateTimeController extends Controller
{
public function index()
{
return view('home');
}
public function addMonth()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->addMonth();
print(""
.print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
}
Define Months with addMonths()
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateTimeController extends Controller
{
public function index()
{
return view('home');
}
public function addMonths()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->addMonths(6);
print(""
.print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
}
Define Sub Month with subMonth()
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateTimeController extends Controller
{
public function index()
{
return view('home');
}
public function addSubMonth()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->subMonth();
print(""
.print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
}
Define Sub Months with subMonths()
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateTimeController extends Controller
{
public function index()
{
return view('home');
}
public function addSubMonths()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->subMonths(4);
print(""
.print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
}
Here is the complete and final code example:
namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateTimeController extends Controller
{
public function index()
{
return view('home');
}
public function addMonth()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->addMonth();
print(""
.print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
public function addMonths()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->addMonths(6);
print("
".print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
public function addSubMonth()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->subMonth();
print("
".print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
public function addSubMonths()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->subMonths(4);
print("
".print_r($nowTimeDate,true)."
");
print("
".print_r($newTime,true)."
");
}
}
We have completed the laravel carbon tutorial, and i believe you must have liked all the examples of adding months with Laravel carbon.
