简体   繁体   中英

How to check the data time is exist in mysql?

There is a timetable system,

each timeslot is only exist one booking,

and I save data like this:

start_time datetime
duration int 

That means, if booking from 2015-12-20 21:00:00 to 2015-12-20 23:00:00 will be

start_time 2015-12-20 21:00:00
duration 3

Now , I would like to check whether the insert data are:

1) time range overlap with exist record?

2) is exceed 24 hour? eg. booking from 22:00 but duration is 5 hour

I attempt the condition like , but can't check with 24 hour

$start_time = new DateTime($_POST['start_time']);
$end_time = new DateTime($booking_time);
$end_time =  $end_time->add(new DateInterval('PT' . $_POST['duration']. 'H'));

$_POST['start_time'] >= start_time AND  end_time <= start_time 

how to fix that?

Thanks for helping

For your second condition, are you trying to check if a booking will end in the same day as it will start?

If that's the case, the following select will return the bookings that matches this condition:

SELECT start_time, duration FROM bookings WHERE DATE(start_time) <> DATE(TIMESTAMPADD(HOUR, duration, start_time));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM