Laravel Carbon Add Years Tutorial with Example

Last Updated on by in Laravel

This tutorial will guide you about adding a year, add years, add sub year, and add years in Laravel application using the PHP Carbon module from scratch.

In Laravel, handling date-time is not such a complicated process, and the credit goes to the Carbon package. You will see the Laravel Carbon add the year, years, sub-year, and sub years example.

The example we are sharing with you built with Laravel; however, you can get the idea and apprehend in older versions of Laravel 7 and 6 without any hesitation.

The carbon is a reliable solution inoculated by PHP DateTime class, and carbon is used in laravel by just defining the controller’s top part.

DateTime is the key to handling events in any application. Generically, you will see how we create a basic laravel application from the beginning, configure a simple environment, and add years and sub years using the Laravel Carbon example.

Enough talk, let’s get started.

Setting up Laravel Project

First step begins with invoking a laravel application installation:

composer create-project laravel/laravel --prefer-dist laravel-carbon-years-example

Get into the project root:

cd laravel-carbon-tutorial

Run the application:

php artisan serve

To check the years use the following URL:

http://127.0.0.1:8000

Next, create a new controller and add years and sub years in Laravel application:

php artisan make:controller DateTimeController

All the below code will go to app/Http/Controllers/DateTimeController.php file:

Define Year with addYear()

Import Carbon class on top of the controller, define function and add year to the current date using addYear() method:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DateTimeController extends Controller
{
    // view
    public function index()
    {
        return view('home');
    }

    // define year
    public function addYear()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addYear();
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }
}

Define Years with addYears()

Similarly, add years method and pass the years numerically in the method:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DateTimeController extends Controller
{
    // view
    public function index()
    {
        return view('home');
    }

    // define years
    public function addYears()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addYears(6);
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

}

Define Sub Year with subYear()

Define the subYear() function using the Carbon object.

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DateTimeController extends Controller
{
    // view
    public function index()
    {
        return view('home');
    }

    // define sub year
    public function addsubYear()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->subYear();
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

}

Define Sub Years with subYears()

You can access subYears() method and pass the number of years:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DateTimeController extends Controller
{
    // view
    public function index()
    {
        return view('home');
    }

    // define sub years
    public function addSubYears()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->subYears(4);
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }    
}

You can compare with the below code of Laravel Carbon add years and sub years example:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;

class DateTimeController extends Controller
{
    // view
    public function index()
    {
        return view('home');
    }

    // define year
    public function addYear()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addYear();
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

    // define years
    public function addYears()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addYears(6);
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

    // define sub year
    public function addsubYear()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->subYear();
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }

    // define sub years
    public function addSubYears()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->subYears(4);
        
        print("<pre>".print_r($nowTimeDate,true)."</pre>");
        print("<pre>".print_r($newTime,true)."</pre>");
    }    
}

Summary

So, this was it. You have seen add years example with the Laravel Carbon module we have added the years, and i hope it will help you handle date-time in Laravel application.