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

Shady Hesham's avatar

whereBetween 2 months

I need to fetch data between 2 months, I have a date picker which the user chose months which goes to the controller as following $from = 01-2022, $to = 10-2022, how to get the data between these months.

the method that I used but not working

$query = Appointment::select('id',) ->whereBetween('created_at', [$from,$to]) ->get()

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104
$query = Appointment::select('id')
    ->whereBetween('created_at', [
        Carbon::createFromFormat('m-Y', $from)->startOfMonth(), //2022-10-01 00:00:00.0
        Carbon::createFromFormat('m-Y', $to)->endOfMonth() // 2022-10-31 23:59:59.999999
    ])
    ->get();
1 like

Please or to participate in this conversation.