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

maplerock's avatar

Between times query

I'm trying to figure out how to query a table in my database for all events that start in the morning so between midnight and say midday tried the following but not having much luck. Any ideas?

$sessions->whereBetween('start_date',array(00:00,12:00));
0 likes
3 replies
topvillas's avatar

I don't think that's a valid date format.

Try 00:00:00 and 12:00:00 and you might need to do some trickery in SQL to cast the dates as time.

Talinon's avatar

If you're dealing with dates and time, I would leverage Carbon.

$start = Carbon::parse()->startOfDay();

$sessions->whereBetween('start_date', [$start, $start->addHours(12)]);

If you're dealing with other date/time formats, you can always call format() on the instance of Carbon to cast it to anything you need.

maplerock's avatar
maplerock
OP
Best Answer
Level 11

Think I've got there:

$sessions->whereRaw("TIME (`start_date`) BETWEEN '{$cut_offs[$filter][0]}' AND '{$cut_offs[$filter][1]}'");

Please or to participate in this conversation.