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

Hujjat's avatar
Level 10

How to Query Yesterdays records ?

Hi,

How can I query yesterday's record only?.

This is what I have done so far, but not working.

'''

$countYesterday = Timer::where('created_at','=', Carbon::yesterday())->get();

'''

Plus, How about this week and This month as well.

Thank's :)

0 likes
9 replies
gabrielbuzzi's avatar

Maybe this

Time::whereRaw("DAY(created_at) = '" . Carbon::yesterday()->format('Y-m-d') . "'")->get()
2 likes
petrit's avatar
petrit
Best Answer
Level 20

Is appearing any error?

This should give all records which have date equal to yesterday

$yesterday = date("Y-m-d", strtotime( '-1 days' ) );
$countYesterday = Timer::whereDate('created_at', $yesterday )->get();

Also take a look at documentation for whereDate, whereMonth, whereYear https://laravel.com/docs/5.3/queries#where-clauses

3 likes
albertflex's avatar

$yesterday = Timer::whereDate('created_at', Carbon::now()->addDay(-1))->get();

1 like
LiamMcArthur's avatar

Here's another clean solution using Carbon:

$yesterday = Timer::whereDate('created_at', Carbon::yesterday())->get();

Please or to participate in this conversation.