In this article, we will see carbon add days to date in laravel. Carbon provides the addDay() and addDays() functions to add days on the carbon date object. You can add a day on the current date using carbon in laravel 7 and laravel 8. Also, you can add days to date using the carbon addDays() function. The addDays() function may require to passing the parameter of a number of days.
So, let's see, how to add days in date in laravel 7/8 and laravel 7/8 add a day to date using carbon.
In this example, we will add a day to the current date. Also, you can add a day to any specific date.
<?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] => 2020-12-02 08:51:57.696013
[timezone_type] => 2
[timezone] => GMT
)
Carbon\Carbon Object
(
[date] => 2020-12-03 08:51:57.696023
[timezone_type] => 2
[timezone] => GMT
)
In this example, we will add three days to the current date.
<?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(3);
print_r($currentDateTime);
print_r($newDateTime);
}
}
Output:
Carbon\Carbon Object
(
[date] => 2020-12-02 08:55:58.896013
[timezone_type] => 2
[timezone] => GMT
)
Carbon\Carbon Object
(
[date] => 2020-12-05 08:55:59.796023
[timezone_type] => 2
[timezone] => GMT
)
You might also like:
In this article, I will explain you how to add ckeditor in laravel, CKEditor is a WYSIWYG rich text edito...
Jun-18-2020
For finding the right web hosting provider, you need to do a lot of research. There are hundreds of web hosting provider...
Nov-12-2021
This article will show drag and drop file upload using dropzone js in laravel 8. DropzoneJS is an open-source library th...
Oct-20-2020
In this article, we will see how to generate barcode using javascript. We will use a javascript plugin to generate...
Nov-01-2022