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

hannylicious's avatar

Trying to query all records for specific date - whereBetween not working?

My Scope:

 public function scopeEventsForDay($query, $fetch_date) {
    return $query->whereBetween('event_date', array("'".$fetch_date."'", "'".$fetch_date." 23:59:59'"->orderby('event_date', 'asc');
    }

I'm trying to query to get all the events for a specific date. The $fetch_date is formatted appropriately and if I build the query directly into SQL like this it works:

SELECT * FROM events WHERE event_date BETWEEN '2016-07-12' AND '2016-07-12 23:59:49'

Any thoughts on the best way to get this accomplished?

0 likes
4 replies
SaeedPrez's avatar
Level 50
 public function scopeEventsForDay($query, $fetch_date) {
    return $query->where('event_date', 'LIKE', $fetch_date . '%')->orderBy('event_date', 'asc'));
}
hannylicious's avatar

Missed a closing ) on my array. All good going forward.

Also, I believe that your method would work, too @SaeedPrez (and it seems to).

SaeedPrez's avatar

@hannylicious I noticed the missing ) but I thought this method would be cleaner since you're not actually interested in different dates or specific times, it would simply just focus on the provided date and ignore the time.

hannylicious's avatar

Yeah, much cleaner and does exactly what I wanted! Thanks again for your help!

1 like

Please or to participate in this conversation.