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

FarhadMohammadi's avatar

Mysql Seperated Date and Time Columns

We have a times table with below structure:

create table times
  (
		id      bigint unsigned primary key,
	    user_id bigint unsigned not null,
		day     date            not null,
		time    time            not null
    )
    collate = utf8mb4_unicode_ci;
	create index on times (day);
	create index times_time_index    on times (time);

day sample is '2022-09-18' and time sample is '13:10:00'

What is best query for selecting times > now()?

0 likes
4 replies
Tray2's avatar

I really suggest you store both values in the same column, because this design is bad. How ever you should be able to do

date(now()), time(now())
1 like
Sinnbeck's avatar

Or (assuming only on that date)

->where('day', now()->toDateString())->where('time', '>', now()->toTimeString())->get()
1 like
Snapey's avatar
Snapey
Best Answer
Level 122

If you want to get records where the date date/time is in the future;

Date needs to be today AND time needs to be later

OR

Date needs to be after today and time can be anything

    ->where(function($query){
		$query->where('day', today())->where('time', '>', now()->format('H:i:s')
	})
	->orWhere('day', '>', today())
    ->get()
1 like

Please or to participate in this conversation.