Carbon Add Months To Date In Laravel 9

Websolutionstuff | Nov-18-2022 | Categories : Laravel PHP

In this article, we will see carbon add months to date in laravel 9. Carbon provides the addMonth() and addMonths() functions to add month to date objects. Using the addMonth() function you can add one month to the date and time object and the addMonths() function helps to add no. of months adds to date and time objects.

So let's see, how to add months in laravel 9 using carbon, add month to date in laravel 9, and PHP add month to date.

Carbon addMonth() Function

In this example, we will add the month to the current date using the carbon addMonth() function.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DateController extends Controller
{
    public function index()
    {
        $currentDateTime = Carbon::now();
        $newDateTime = Carbon::now()->addMonth();
             
        print_r($currentDateTime);
        print_r($newDateTime);
    }
}

 

Output:

Carbon\Carbon Object
(
    [date] => 2022-11-03 09:31:35.635461

    [timezone_type] => 2

    [timezone] => GMT
)

Carbon\Carbon Object
(
    [date] => 2022-12-03 09:31:36.435461

    [timezone_type] => 2

    [timezone] => GMT
)

 

 

Carbon addMonths() Function

In this example, we will add 5 months to the current date using the carbon addMonths() function.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DateController extends Controller
{
    public function index()
    {
        $currentDateTime = Carbon::now();
        $newDateTime = Carbon::now()->addMonths(5);
             
        print_r($currentDateTime);
        print_r($newDateTime);
    }
}

 

Output:

Carbon\Carbon Object
(
    [date] => 2022-11-03 09:35:23.335461

    [timezone_type] => 2

    [timezone] => GMT
)

Carbon\Carbon Object
(
    [date] => 2023-04-03 09:35:23.535861

    [timezone_type] => 2

    [timezone] => GMT
)

 

 

Carbon diffForHumans with addMonth() Function

In this example, we will use the diffForHumans() function with addMonth() to the current date.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DateController extends Controller
{
    public function index()
    {
        $currentDateTime = Carbon::now();
        $newDateTime = $currentDateTime->diffForHumans($currentDateTime->copy()->addMonth());
             
        print_r($currentDateTime);
        print_r($newDateTime);
    }
}

 

Output:

2022-11-03 07:10:44
1 month before

 

Carbon diffForHumans with addMonths() Function

In this example, we will use the diffForHumans() function with addMonths() to the current date.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DateController extends Controller
{
    public function index()
    {
        $currentDateTime = Carbon::now();
        $newDateTime = $currentDateTime->diffForHumans($currentDateTime->copy()->addMonths(5));
             
        print_r($currentDateTime);
        print_r($newDateTime);
    }
}

 

Output:

2022-11-03 07:11:09
5 months before

 


You might also like:

Recommended Post
Featured Post
How To Validate 10 Digit Mobile Number Using Regular Expression
How To Validate 10 Digit Mobil...

In This small tutorial, I will explain to you how to validate a 10-digit mobile number using regular expressions in...

Read More

Jun-15-2020

How To Create Line Chart In Laravel 9 Using Highcharts
How To Create Line Chart In La...

In this article, we will see how to create a line chart in laravel 9 using highcharts. A line chart is a graph...

Read More

Oct-04-2022

Laravel 9 Order By Query Example
Laravel 9 Order By Query Examp...

In this article, we will see laravel 9 order by query example. how to use order by in laravel 9.The orderBy me...

Read More

Mar-28-2022

Laravel 8 Has Many Through Relationship Example
Laravel 8 Has Many Through Rel...

In this example we will see laravel 8 has many through relationship example. hasManyThrough relationship difficult to un...

Read More

Nov-17-2021