Laravel Carbon Add Months Tutorial with Example | How to Add Month with Laravel Carbon

Last updated on by Digamber

The primary topic of this tutorial is to add months in the Laravel 8 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 8 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.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DateTimeController extends Controller
{
    // view
    public function index()
    {
        return view('home');
    }

    // define month
    public function addMonth()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addMonth();
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

}

Define Months with addMonths()

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DateTimeController extends Controller
{
    // view
    public function index()
    {
        return view('home');
    }

    // define months
    public function addMonths()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addMonths(6);
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

}

Define Sub Month with subMonth()

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DateTimeController extends Controller
{
    // view
    public function index()
    {
        return view('home');
    }

    // define sub month
    public function addSubMonth()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->subMonth();
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }
      
}

Define Sub Months with subMonths()

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DateTimeController extends Controller
{
    // view
    public function index()
    {
        return view('home');
    }

    // define sub months
    public function addSubMonths()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->subMonths(4);
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }    

}

Here is the complete and final code example:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DateTimeController extends Controller
{
    // view
    public function index()
    {
        return view('home');
    }

    // define month
    public function addMonth()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addMonth();
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

    // define months
    public function addMonths()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addMonths(6);
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

    // define sub month
    public function addSubMonth()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->subMonth();
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

    // define sub months
    public function addSubMonths()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->subMonths(4);
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }    

}

We have completed the laravel carbon tutorial, and i believe you must have liked all the examples of adding months with Laravel carbon.