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

skeith22's avatar

where between in query builder using date values is not working as expected.

I'm trying to get the data between 2 dates.

and this code returns 0 results

return Model::whereBetween('created_at', [$this->carbon->startOfWeek(), $this->carbon->endOfWeek()])->get();

this is the query statement it creates when I use toSql();

select * table where created_at between ? and ?

but this query works

return $this->appointment->whereRaw('`created_at` between "' . $this->carbon->startOfWeek() . '" and "' . $this->carbon->endOfWeek() . '"')->get();

this is the query statement it creates when I use toSql();

select * table where created_at between "2017-07-31 00:00:00" and "2017-08-06 23:59:59"

I don't get why the first one doesn't work but doing whereRaw works but they have the same query statement.

Its exactly the same and its bothering me.

is this a bug?

0 likes
1 reply
skeith22's avatar
skeith22
OP
Best Answer
Level 22

Nevermind guys I found the answer I just needed to add

->toDateTimeString():
3 likes

Please or to participate in this conversation.