Is there an efficient way of calculating whether 2 or more dates overlap in PHP?
Ex.
- Date1: Start1 = 2012-03-10 / End1 = 2012-05-10
- Date2: Start2 = 2012-04-25 / End2 = 2012-06-01
- Date3: Start3 = 2012-07-15 / End3 = 2012-08-20
In the example above Date1 and Date2 overlaps, Date3 is OK.
If your dates are in YYYY-MM-DD format then you can just do a string comparison (they dont have to be valid dates either OR after 1970 if using strtotime() for unix timestamps)
this checks if start or end of date1 is between the start and end of date2 OR if date1 surrounds date2
.
thanks to @Germann-Arlington –