Add or minus years to date in laravel

The best part of Laravel is it has verity of open source plugins available. Open of them is Carbon. Carbon is date time package for Laravel or any other PHP based frameworks.

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

For examples, 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

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

Let's take example to understand both function.

Output :

In above example, we have create two carbon object. First object will define current date while in second object custom date is set. Here, we will use addYear() and addYears() function to increase date value. In addYears() function, we statically passed five as value so it will increase five years in our custom date.

Deduct Year or Years to Carbon Date In Laravel

Same as adding years, carbon also provides per-defined function to decrease years from carbon date. The subYear() and subYears() function is used to deduct year or years from carbon date. Here subYears() function will take integer input.

Let's take example to understand both function.

Output :

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


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

Share your thoughts

Ask anything about this examples