In this article, we will see carbon add day and add days to date in laravel 9. Carbon provides the addDay() and addDays() methods to add days to date objects. You can add a single day and also you can add days to the current date or any date. In the addDays() function need to pass the no. of days as a parameter.
So, let's see laravel 9 carbon add days to date, how to add a day to date in laravel 9, PHP carbon add a day.
In this example, we will add a day to the current date using the carbon addDay() function.
<?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()->addDay();
print_r($currentDateTime);
print_r($newDateTime);
}
}
Output:
Carbon\Carbon Object
(
[date] => 2022-11-02 08:51:57.696013
[timezone_type] => 2
[timezone] => GMT
)
Carbon\Carbon Object
(
[date] => 2022-11-03 08:51:57.696023
[timezone_type] => 2
[timezone] => GMT
)
In this example, we will add 5 days to the current date using the carbon addDays() function.
<?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()->addDays(5);
print_r($currentDateTime);
print_r($newDateTime);
}
}
Output:
Carbon\Carbon Object
(
[date] => 2022-11-02 08:55:58.896013
[timezone_type] => 2
[timezone] => GMT
)
Carbon\Carbon Object
(
[date] => 2022-11-07 08:55:59.796023
[timezone_type] => 2
[timezone] => GMT
)
You might also like:
As an Angular 15 developer, I understand the significance of incorporating file upload functionality into web applicatio...
Jun-19-2023
Livewire is a powerful framework for building dynamic, reactive components in Laravel without writing any JavaScript. Wi...
Jan-28-2025
In this article, we will see how to create web notifications in laravel 9 using pusher. Here, we will learn how to...
Feb-03-2023
In this article, we will see laravel 10 send bulk mail using a queue. Here, we will learn about how to send bulk ma...
Mar-13-2023