Laravel-9 Tutorial – Laravel Carbon Add Years Tutorial with Example

This tutorial will guide you about adding a year, add years, add sub year, and add years in Laravel 9 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:


namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateTimeController extends Controller
{
    
    public function index()
    {
        return view('home');
    }
    
    public function addYear()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addYear();
        
        print("
".print_r($nowTimeDate,true)."

");
print("

".print_r($newTime,true)."

");
}
}

Define Years with addYears()

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


namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateTimeController extends Controller
{
    
    public function index()
    {
        return view('home');
    }
    
    public function addYears()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addYears(6);
        
        print("
".print_r($nowTimeDate,true)."

");
print("

".print_r($newTime,true)."

");
}
}

Define Sub Year with subYear()

Define the subYear() function using the Carbon object.


namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateTimeController extends Controller
{
    
    public function index()
    {
        return view('home');
    }
    
    public function addsubYear()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->subYear();
        
        print("
".print_r($nowTimeDate,true)."

");
print("

".print_r($newTime,true)."

");
}
}

Define Sub Years with subYears()

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


namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateTimeController extends Controller
{
    
    public function index()
    {
        return view('home');
    }
    
    public function addSubYears()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->subYears(4);
        
        print("
".print_r($nowTimeDate,true)."

");
print("

".print_r($newTime,true)."

");
}
}

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


namespace AppHttpControllers;
use IlluminateHttpRequest;
use CarbonCarbon;
class DateTimeController extends Controller
{
    
    public function index()
    {
        return view('home');
    }
    
    public function addYear()
    {
        $nowTimeDate = Carbon::now();
        $newTime = Carbon::now()->addYear();
        
        print("
".print_r($nowTimeDate,true)."

");
print("

".print_r($newTime,true)."

");
}

public function addYears()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->addYears(6);

print("

".print_r($nowTimeDate,true)."

");
print("

".print_r($newTime,true)."

");
}

public function addsubYear()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->subYear();

print("

".print_r($nowTimeDate,true)."

");
print("

".print_r($newTime,true)."

");
}

public function addSubYears()
{
$nowTimeDate = Carbon::now();
$newTime = Carbon::now()->subYears(4);

print("

".print_r($nowTimeDate,true)."

");
print("

".print_r($newTime,true)."

");
}
}

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.

Leave a Reply

Your email address will not be published. Required fields are marked *