How to Count Days Between Two Dates using Laravel Carbon

Sovary November 4, 2022 587
1 minute read

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...

Laravel  PHP  Laravel 9  Carbon Laravel 
Author

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him