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

Bossino's avatar

where date between created_at

Hello. I want to get the results of posts between dates in created_at. The problem is the created_at is a timestamp. I want to compare it with its date value only.

Post::whereBetween('created_at', ['2022-09-25', '2022-09-28'])->get()

Tried that one but it no results returned.

0 likes
5 replies
Sinnbeck's avatar

Pretty sure that should work. Did you change the format of the column? Is it mysql?

kokoshneta's avatar

Have you tried using date objects?

Post::whereBetween('created_at', [Carbon::parse('2022-09-25'), Carbon::parse('2022-09-28')])->get();

And of course, have you checked to make sure that there actually are any records in the table whose created_at field is between those two dates?

rodrigo.pedra's avatar

@sinnbeck if timestamps are stored as ints it won't work automatically -- I've seen that, argument is that was better for indexing, which doesn't seem to be true from my searches about it.

@bossino can you try this:

Post::query()
    ->whereBetween('created_at', [
        \Carbon\Carbon::createFromFormat('Y-m-d|', '2022-09-25')->startOfDay()->timestamp,
        \Carbon\Carbon::createFromFormat('Y-m-d|', '2022-09-28')->endOfDay()->timestamp,
    ])
    ->get();

Please or to participate in this conversation.