I need some logic on this one..
I have a My-SQL table as follows
id | program_name | start_date | end_date
1 | program1 | 2014-12-01 | 2014-12-07
Now if I want to insert a new record by PHP for id 1 I want to check the period (between start_date and end_date) overlaps or no..
Lets say I want to insert a new record with
id = 1 , program_name = program1 , start_date = 2014-12-06 , end_date = 2014-12-10
As the period overlaps here I want to throw a warning message. I need logic for that.
What I have come up is,
Let
start_date in database = old_start_date , end_date in database = old_end_date, the insert record start date = new_start_date , insert record end date = new_end_date
if
old_start_date < new_start_date and old_end_date < new_end_date == fine (period before the entered period)
old_start_date > new_start_date and old_end_date > new_end_date == fine (period after the entered period)
old_start_date < new_start_date and old_end_date > new_end_date == warning (old period is within the new period)
old_start_date > new_start_date and old_end_date < new_end_date == warning (new period is within the old period)
old_start_date = new_start_date == warning
old_end_date = new_end_date == warning
Please guide me if I am wrong or would there be a more efficient way of doing this either by My-SQL or by PHP.
Thanks.