Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dixitchopra's avatar

Compare dates in Eloquent

How to write an Eloquent query to search created_at between two dates? Basically, I am trying to get sales records between two days. e.g. 24/05/2015 to 25/05/2015 or today's sale.

0 likes
3 replies
devratna's avatar
/*Not sure if we need to use get() method though, you can use a Carbon instances with the dates if you want*/
$sales = Sales::whereBetween('created_at', [$date1, $date2])->get();

If you meant sales count then.

$sales_count = Sales::whereBetween('created_at', [$date1, $date2])->count(); 

I might be wrong

constb's avatar
constb
Best Answer
Level 6

@dixitchopra $model->whereBetween('created_at', [ new Carbon($date_from), new Carbon($date_to) ]);

2 likes
dixitchopra's avatar

Thanks. How can I find whether the date I have given is a valid date? $date_from = '01-01-2015' or $date_from = 'false'; I am getting InvalidArgumentException if I provide any wrong input. Is there any way to validate date?

Please or to participate in this conversation.