DEV Community

loading...

Carbon Add Years To Date In Laravel

Websolutionstuff
I am Laravel Developer. I have good knowledge of JavaScript, JQuery, Bootstrap and REST API.
Originally published at websolutionstuff.com ・1 min read

Today we will see example of carbon add years to date in laravel, here i will give you simple example of carbon in laravel, carbon provides many funaction like addyear(), addyears() to add year in laravel.

If we need to add year or more then one years in date then you can use carbon in laravel. carbon provides addyear() and addyears() method to add years on carbon date object.

addYear() Example

<?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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output :

Carbon\Carbon Object
(
    [date] => 2020-12-02 09:45:30.376461

    [timezone_type] => 2

    [timezone] => GMT
)

Carbon\Carbon Object
(
    [date] => 2021-12-02 09:45:30.376461

    [timezone_type] => 2

    [timezone] => GMT
)
Enter fullscreen mode Exit fullscreen mode

addYears() Example

<?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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output :

Carbon\Carbon Object
(
    [date] => 2020-12-02 09:46:32.406561

    [timezone_type] => 2

    [timezone] => GMT
)

Carbon\Carbon Object
(
    [date] => 2023-12-02 09:46:50.454561

    [timezone_type] => 2

    [timezone] => GMT
)
Enter fullscreen mode Exit fullscreen mode

Read Also : How To Get Selected Checkbox List Value In JQuery


Read Also : How To Delete Multiple Records Using Checkbox In Laravel

Thanks for the reading...!!

Discussion (0)