Carbon Add Years To Date In Laravel
In this tutorial, we will see an example of carbon add years to date in laravel, here I will give you a simple example of carbon in laravel, carbon provides many functions like addyear(), addyears() to add the year in laravel 8. Using the carbon addyear() function you can change the year in the date in laravel 8.
If we need to add a year or more than one years in date then you can use carbon in laravel. carbon provides addyear() and addyears() method to add years on carbon date object.
So, let's see an example of laravel 8 carbon add years to date or add year using carbon in laravel 8. addYear() Example
The carbon addYear() function is used to add the year on a date. So, here I will give you an example of the current date to add a year.
<?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);
}
}
Output :
Current Date and Time : 2022-01-16 08:13:07
Current Date with Add Year : 2023-01-16 08:13:07
addYears() Example
The Carbon addYear() function is used to add year as per your requirements on the date. So, here I will give you an example of the current date to add 5 years.
<?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(5);
print_r($currentDateTime);
print_r($newDateTime);
}
}
Output :
Current Date and Time : 2022-01-16 08:14:46
Current Date with Add Years : 2027-01-16 08:14:46
You might also like :