How to get the data between two dates and times in laravel?

Member

by daisha , in category: PHP Frameworks , 6 months ago

How to get the data between two dates and times in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by ryan.murray , 6 months ago

@daisha 

To get data between two dates and times in Laravel, you can use the following code:

1
2
3
4
5
6
7
use AppModelsModelName;
use CarbonCarbon;

$startDateTime = Carbon::parse('2022-01-01 00:00:00');
$endDateTime = Carbon::parse('2022-01-31 23:59:59');

$data = ModelName::whereBetween('created_at', [$startDateTime, $endDateTime])->get();


In this code snippet, replace ModelName with the name of your model and created_at with the column containing the date and time you want to filter by. Adjust the $startDateTime and $endDateTime variables to the desired start and end dates and times. The whereBetween method will retrieve records where the created_at column value falls within the specified range.