Kyle Adams Kyle Adams - 6 months ago 76
SQL Question

Get only records created today in laravel

How do I use the

created_at
field to get only the records that were created today and no other day or time?

I was thinking of a
->where('created_at', '>=', Carbon::now())
But Im not sure that would work.

Answer

You can use whereRaw('date(created_at) = curdate()') if timezone is not a concern or whereRaw('date(created_at) = ?', [Carbon::now()->format('Y-m-d')] ) otherwise.

Since the created_at field is a timestamp, you need to get only the date part of it and ignore the time part.

Comments