Add or minus years from date using carbon in laravel

The best part of Laravel is it has a variety of open-source plugins available. One of them is Carbon. Carbon is a date-time package for Laravel or any other PHP-based framework.

In this blog, we will see some practical examples to add or remove years from carbon date objects. Carbon provides a function for adding or removing year/years. In carbon, you can add single or multiple years directly to date using these functions or subtract years easily.

For example, here we have used Test Controller. You can implement this code into any other controller, class, or even view.

Add Year or Years to Carbon Date In Laravel

Carbon provides two functions to increase year in date. which are addYear() and addYears(). The addYear() function will automatically increase one year-to-date object. On the other hand addYears() function will accept one integer parameter and add years based on value.

Let’s take an example to understand both functions.

<?php

namespace App\Http\Controllers;

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

class TestController extends Controller
{
    public function index(){
        //Add single year
        $today = Carbon::now();
        $nextYear = $today->addYear();

        //Add Multiple Year
        $customDate = Carbon::createFromFormat('Y-m-d', '2022-06-30');
        $next5Year = $customDate->addYears(5);

        dd($nextYear, $next5Year);
    }
}

Output :

date: 2023-06-30 18:22:08.453281 UTC (+00:00)
date: 2027-06-30 18:22:08.0 UTC (+00:00)

In the above example, we have created two carbon objects. The first object will define the current date while in the second object, custom date is set. Here, we will use the addYear() and addYears() functions to increase the date value. In addYears() function, we statically passed five as a value so it will increase by five years in our custom date.

Deduct Year or Years to Carbon Date In Laravel

Similar to adding years, carbon also provides a pre-defined function to decrease years from the carbon date. The subYear() and subYears() function is used to deduct years or years from carbon date. Here subYears() function will take integer input.

Let’s take an example to understand both functions.

<?php

namespace App\Http\Controllers;

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

class TestController extends Controller
{
    public function index(){
        //Minus single year
        $today = Carbon::now();
        $pastYear = $today->subYear();

        //Minus Multiple Year
        $customDate = Carbon::createFromFormat('Y-m-d', '2022-06-30');
        $past5Year = $customDate->subYears(5);

        dd($pastYear, $past5Year);
    }
}

Output :

date: 2021-06-30 18:30:12.802047 UTC (+00:00)
date: 2017-06-30 18:30:12.0 UTC (+00:00)

Here, we just changed functions in the previous example and it will deduct year/years based on our logic.

Conclusion

In this article, you have learned to add or remove year/years from carbon date. Here, we have taken simple examples to explain the topic but you can use those functions as per your requirements.