Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel, filter timestamp by date only

Tags:

php

laravel

I want to filter a table by user ID and the created_at field. The only thing is created_at is a timestamp, and I want to query by date only not time.

So something like -

$messages = Message::where(['from' => $fromUser->id, 'created_at' => 'TODAY'S DATE'])->get();
like image 437
colouredFunk Avatar asked Oct 20 '15 08:10

colouredFunk


People also ask

What is the use of timestamps in laravel?

Timestamps allows you to automatically record the time of certain events against your entities. This can be used to provide similar behaviour to the timestamps feature in Laravel's Eloquent ORM.

How do I get rid of time stamp in laravel?

You either have to declare public $timestamps = false; in every model, or create a BaseModel, define it there, and have all your models extend it instead of eloquent.


1 Answers

$query->whereDate('created_at', '=', date('Y-m-d'));

or

$query->whereDate('created_at', '=', Carbon::today()->toDateString());
like image 70
Hamid Parchami Avatar answered Oct 18 '22 10:10

Hamid Parchami