Laravel Carbon Add Months Example Tutorial

Aug 07, 2021 . Admin



Hi All,

Today,In this Example i will explain how to add months in laravel using carbon in laravel so it can easy to use in laravel app.

Whenever,if you require to add month or more months you can use carbon in laravel. carbon provide inbuilt function addMonth()

and addMonths() method to add months on carbon date object. so let's see some examples to add month and months.

Here, I will give you full example for how to add months in laravel using carbon in laravel. so follow my example code and you can use your laravel application copy that code.

Example : 1
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon add days to date..
     *
     * @return string
    */
    public function index()
    {
        $currentDateTime = Carbon::now();
        $newDateTime = Carbon::now()->addMonth();

        // echo "<pre />";
        print_r($currentDateTime);
        print_r($newDateTime);
    }

}
Output
Carbon\Carbon Object

(
    [date] => 2021-08-07 08:52:09.117409

    [timezone_type] => 3

    [timezone] => UTC
)

Carbon\Carbon Object

(
    [date] => 2021-09-07 08:52:09.117416

    [timezone_type] => 3

    [timezone] => UTC
)
Example : 2
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class HomeController extends Controller
{
    /**
     * laravel carbon add days to date..
     *
     * @return string
    */
    public function index()
    {
        $currentDateTime = Carbon::now();
        $newDateTime = Carbon::now()->addMonths(2);
        
        // echo "<pre />";
        print_r($currentDateTime);
        print_r($newDateTime);
    }

}
Output
Carbon\Carbon Object

(
    [date] => 2021-08-07 08:52:09.117409

    [timezone_type] => 3

    [timezone] => UTC
)

Carbon\Carbon Object

(
    [date] => 2021-10-07 08:55:06.477737

    [timezone_type] => 3

    [timezone] => UTC
)

I Hope It Will Help You..

#Laravel 8 #Laravel