Suppose you have to create an agenda app, that allows it's users to create meetings or appointments and it must not allow two events to happen at the same time, for simplicity let's just say "all events created must not be in schedule conflict with other events"
It seems simple to resolve at first glance, but allow me to show you some examples of why it is not: Suppose you have two events A which last 60 minutes and B which last 90 minutes.
There are so many ways that we can create this two events in our agenda so that there will be a schedule conflict (date collision)
- Setting A to start and same time as B http://screencloud.net/v/ChX3
- Setting A to start a few minutes before B http://screencloud.net/v/73vI
- Setting B to start a few minutes before A http://screencloud.net/v/uyaG
- Setting A to start a few minutes later of B http://screencloud.net/v/lpLg
If you try to list all possible combinations in which A and B are in conflict your code may become pretty big and very difficult to maintain for something as simple as "detect date collisions", luckily there is another way more simple: Instead of search all possible conditions in which A and B are in conflict lets search for the possibles situations in which they are not
- A starts and ends before B http://screencloud.net/v/hEXt
- B starts and ends before A http://screencloud.net/v/tR6X
We can use this conditions and some boolean logic to create for example a mysql query:
I hope this is useful for anyone of you.