Carbon Add Years To Date In Laravel 9

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

In this article, we will see carbon add years to date in laravel 9. Carbon provides addYear() and addYears() functions to add a year to date objects. You can add a year to the current date and any other date. In the addYears() function need to pass the no. year as a parameter to date object.

So, let's see, how to add a year to date in laravel 9, PHP carbon add a year, and laravel 9 carbon add years to date.

Carbon addYear() Function

In this example, we will add a year to the current date object using the carbon addYear() 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()->addYear();
             
        print_r($currentDateTime);
        print_r($newDateTime);
    }
}

 

Output:

Carbon\Carbon Object
(
    [date] => 2022-11-03 08:01:50

    [timezone_type] => 2

    [timezone] => GMT
)

Carbon\Carbon Object
(
    [date] => 2023-11-03 08:01:50

    [timezone_type] => 2

    [timezone] => GMT
)

 

 

Carbon addYears() Function

In this example, we will add 3 years to the current date using the carbon addYears() 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()->addYears(3);
             
        print_r($currentDateTime);
        print_r($newDateTime);
    }
}
 

 

Output:

Carbon\Carbon Object
(
    [date] => 2022-11-03 08:03:52

    [timezone_type] => 2

    [timezone] => GMT
)

Carbon\Carbon Object
(
    [date] => 2025-11-03 08:03:52

    [timezone_type] => 2

    [timezone] => GMT
)

 

 

Carbon diffForHumans with addYear() Function

In this example, we will use the diffForHumans() function with addYear() 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()->addYear());
             
        print_r($currentDateTime);
        print_r($newDateTime);
    }
}

 

Output:

2023-11-03 08:11:46
1 year before

 

Carbon diffForHumans with addYears() Function

In this example, we will use the diffForHumans() function with addYears() 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()->addYears(3));
             
        print_r($currentDateTime);
        print_r($newDateTime);
    }
}

 

Output:

2025-11-03 08:11:11
3 years before

 


You might also like:

Recommended Post
Featured Post
How To Get Current Month Records In MySQL
How To Get Current Month Recor...

In this tutorial, we will see how to get current month records in MySQL. For data analysis and reporting, we need more d...

Read More

Feb-08-2022

Laravel 9 One To Many Polymorphic Relationship
Laravel 9 One To Many Polymorp...

In this article, we will see laravel 9 one to many polymorphic relationship. A one-to-many polymorphic relation is...

Read More

Apr-05-2022

How Generate PDF From HTML View In Laravel
How Generate PDF From HTML Vie...

In this example, I will teach you how to generate PDF files from an HTML view in laravel. For generating PDF files I wil...

Read More

Jun-01-2020

Laravel 8 Image Upload Validation
Laravel 8 Image Upload Validat...

In tutorial we will see how to validate laravel 8 image upload validation. In laravel 7/8 you can validate image using t...

Read More

Dec-15-2021