Categories
Laravel

How to Retrieve data between two dates in Laravel, PHP

In this tutorial, you will learn how easy it is to get data between two dates from the database using Laravel’s whereBetween method.

Let’s say you have an online store with orders data in the table and you want to get all the orders between two dates. In this scenario, where between method is the best fit. So let’s see it in action.

Example with whereBetween

$from = Carbon::now()->subDays(5);
$to = Carbon::now();

$orders = Order::whereBetween('created_at', [$from, $to])
                ->get();

dd($orders);

Example with where

$from = Carbon::now()->subDays(5);
$to = Carbon::now();
$orders = Order::where('created_at',$from)
        ->where('created_at',$to)
        ->get();

dd($orders);

By Praveen Anaparthi

11+ years of IT industry experience holding a wide range of skill sets and roles with significant work on PHP, Node.js, Python, Ruby, Docker, React.js, Microsoft Azure, Azure DevOps, Windows PowerShell, Shell script, Jenkins, MongoDB, SQL, MySQL, Apache, Nginx. etc. It is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.

Leave a Reply

Your email address will not be published.