The simple guide of how to use Carbon to count different two days. We will help you to give an example and calculate days between two date in Laravel. Let's check and discuss about how to calculate days between two date with Carbon class. Following example below you will see how we calculate between dates to get days.
The below example will show you how we count days between two date, so we will easily get number of days by caculate with simple function build-in Laravel. You can apply this example below with Laravel 6, Laravel 7, Laravel 8, Laravel 9 application, because this Carbon class is build-in Illuminate\Support\Carbon
. We hope you can understand this simple method.
We will use two variables to defined start_date and end_date to store sample date, so when real project you may apply with your record date and current date. In Laravel we can use Carbon class with function diffInDays()
by using this, you can get the difference between two dates in days. I hope you will find your solution with example below.
diffInDays() - used to count days which accept parameter end date. For example below I created a controller and write print out to test.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
class TestController extends Controller
{
public function index()
{
$start = Carbon::parse("2022-11-02");
$end = Carbon::parse("2022-11-21");
$diffDays = $start->diffInDays($end);
dd($diffDays);
}
}
Output
18
Thanks for reading this page, hope you find solution to apply your project. Please have a nice day!!!
You may also like...
Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him