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

mediax's avatar

DB::table request get only date from "created_at" field?

I want to create a stat how many items are created by user today, but the problem is the "created_at" field has also a time, how can I do this request with ignoring time?

DB::table('users')
->join('items', 'users.id', '=', 'items.user_id')->select('*')
->where('items.user_id', '=', $user_id)->where('categories.created_at', '=', Carbon::now()->format('Y-m-d'))
0 likes
4 replies
Screenbeetle's avatar

Eloquent has built in ways to get just the year and month. Have a play around like this:

->whereYear('created_at', '=', $year)
->whereMonth('created_at', '=', $month)
->whereDay etc

Edit: I think you can also do this:

->whereDate('created_at', '2016-12-31') //from the docs
tykus's avatar

Carbon::today() will give you a Carbon object from 00:00:00 on topday's date; then just use >= to fetch all of the records created today.

DB::table('users')
    ->join('items', 'users.id', '=', 'items.user_id')
    ->where('items.user_id', '=', $user_id)
    ->where('items.created_at', '>=', Carbon::today)
    ->get()
2 likes

Please or to participate in this conversation.