How To Get Data Between Two Dates In Laravel 9

Websolutionstuff | Dec-19-2022 | Categories : Laravel MySQL

In this article, we will see how to get data between two dates in laravel 9. Here we will learn how to count days between two dates in laravel 8/9. You can use a different method to get data between two dates in laravel 7, laravel 8, and laravel 9.

We will use carbon for the date and time to parse the date in laravel 9. Carbon is a simple API extension of DateTime. Carbon makes it easy to get a Date and Time. Also, Carbon provides multiple methods to get data like today's date, yesterday's date, and many other methods to get dates, months, and years.

So, let's see how to get data between two dates in laravel 8/9 and laravel 9 carbon to get the number of days between the two dates.

Example 1: Count Days Between Two Dates in Laravel

In this example, we will count the days between two dates using carbon.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DateController extends Controller
{
    public function index(Request $request)
    {
        $toDate = Carbon::parse("2022-11-26");
        $fromDate = Carbon::parse("2022-11-28");
  
        return $toDate->diffInDays($fromDate);  
    }
}

 

Example 2: Count Months Between Two Dates in Laravel

In this example, we will count the months between two dates using carbon.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DateController extends Controller
{
    public function index(Request $request)
    {
        $toDate = Carbon::parse("2022-06-06");
        $fromDate = Carbon::parse("2022-11-06");
  
        return $toDate->diffInMonths($fromDate);  
    }
}

 

 

Example 3: Count Years Between Two Dates in Laravel

In this example, we will count the years between two dates using carbon.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DateController extends Controller
{
    public function index(Request $request)
    {
        $toDate = Carbon::parse("2020-02-07");
        $fromDate = Carbon::parse("2022-11-28");
  
        return $toDate->diffInYears($fromDate);  
    }
}

 

Example 4: Count the Days Between today's Date

In this example, we will count the days between today's date and the given date using carbon.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DateController extends Controller
{
    public function index(Request $request)
    {
        $date = Carbon::parse("2021-11-28");

        return today()->diffInDays($date);  
    }
}

 


You might also like:

Recommended Post
Featured Post
How To Send Email In Laravel 9 Using Mailtrap
How To Send Email In Laravel 9...

In this article, we will explore the process of sending emails in Laravel 9 using Mailtrap. We will delve into how Larav...

Read More

Jul-27-2022

How To Install React JS Step By Step
How To Install React JS Step B...

In this article, we will see how to install React JS step by step. we will see how to set up an environment fo...

Read More

Aug-08-2022

Node.js Express CRUD Example with MySQL
Node.js Express CRUD Example w...

Hello Guys, In this tutorial we will see how to perform Node js Express CRUD example with mysql. Node js Express ...

Read More

Aug-02-2021

Laravel 8 Order By Query Example
Laravel 8 Order By Query Examp...

In this example we will see laravel 8 order by query example. how to use order by in laravel 8.The orderBy met...

Read More

Dec-01-2021