How To Get Data Between Two Dates In Laravel 10

In this article, we will see how to get data between two dates in laravel 10. Here we will learn to get filter records between two dates in laravel 11. You can use a different method to get data between two dates in laravel 10 and laravel 11.

Laravel also provides a different method to filter records for a date like whereBetween(), where(), and whereDate() eloquent function.

We will use carbon for the date and time to parse the date in laravel 10/11. 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 filter the records between two dates in laravel 10 and Laravel 11 search data between two dates, wherebetween laravel 10 and laravel 11.

Example 1:

In this example, we will filter records using the whereBetween() function.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
use Carbon\Carbon;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $startDate = Carbon::createFromFormat('d/m/Y', '01/04/2023');
        $endDate = Carbon::createFromFormat('d/m/Y', '16/04/2023');
  
        $users = User::select('id', 'name', 'email', 'created_at')
                        ->whereBetween('created_at', [$startDate, $endDate])
                        ->get();
  
        dd($users);
    }
}

 

Example 2:

In this example, we will get data between two dates using the where() function.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
use Carbon\Carbon;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $startDate = Carbon::createFromFormat('d/m/Y', '01/04/2023');
        $endDate = Carbon::createFromFormat('d/m/Y', '16/04/2023');
  
        $users = User::select('id', 'name', 'email', 'created_at')
                        ->where('created_at', '>=', $startDate)
                        ->where('created_at', '<=', $endDate)
                        ->get();
  
        dd($users);
    }
}

 

 

Example 3:

In this example, we will use the whereDate() function and get the data between two dates.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
use Carbon\Carbon;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $startDate = '01/04/2023';
        $endDate = '16/04/2023';
  
        $users = User::select('id', 'name', 'email', 'is_trial_subscription')
                        ->whereDate('is_trial_subscription', '>=', $startDate)
                        ->whereDate('is_trial_subscription', '<=', $endDate)
                        ->get();
  
        dd($users);
    }
}

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS