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

cklester's avatar

Filter Database Query By Date and Time Fields

I have a date field and time field with event information. I need to show all events that are in the future ( > now() ). How can I combine the date and time fields to create a datetime that can be used by Carbon to compare with now()?

I wouldn't mind switching to a datetime field, if that would be easier, but would I still be able to use mass assignment with create and edit forms?

0 likes
4 replies
vipin93's avatar
$now = \Carbon\Carbon::now()->date();

$query = DB::table('events')->whereDate('date','>',$now)->get();
cklester's avatar

Yes, but, the problem is, I have the date and time in two separate fields, so I can't just compare one of the fields. I have to use both. So, how do I combine them in my SQL query to make the comparison?

If I had one datetime field, I could do this:

->where(\Carbon\Carbon::parse('event_date'),'>',\Carbon\Carbon::now())

But I have both an 'event_date' AND 'event_time' field. I'm doing this because it doesn't seem I can do a mass assignment with a datetime HTML form field (there isn't one).

Is there a way to combine two form fields into one database field entry when using forms? That would be very helpful.

cklester's avatar

OK, just discovered the "datetime-local" input field type. I suspect it's just what I need!

Please or to participate in this conversation.