Ad

How To Validate Crossed Time Laravel

- 1 answer

I have the time data in my database like this

+-------------+------------+------------+
|    id       | time_start | time_end   |
+---------------------------------------+
|    1        |  07:00     |  10:00     |
|    2        |  12:00     |  14:00     |
|             |            |            |
|             |            |            |
|             |            |            |
|             |            |            |
|             |            |            |
|             |            |            |
+-------------+------------+------------+

but i'm insert time crossed,

time_start : 08:00
time_end : 09:00

or

time_start : 08:00
time_end : 11:00

How to validate if the time_start and time_end not between date in database ?

Ad

Answer

I give you the way to do that in raw SQL, feel free to translate it into eloquent rigmarole:

SET @tstart='10:00';
SET @tend='11:00';

INSERT INTO t_time_range (time_start, time_end)
SELECT @tstart, @tend 
WHERE  NOT EXISTS
       ( SELECT 1 
         FROM   t_time_range
         WHERE  time_start >= @tstart AND time_start < @tend
         OR     time_end > @tstart    AND time_end <= @tend
         OR     @tstart >= time_start AND @tstart < time_end
       );
Ad
source: stackoverflow.com
Ad