In mysql, how can I check if two date ranges overlap?
I have this:
Note: We have that p.date_started <= p.date_finished
but dateA
I think you have one extra test. The first two statements will cover if the two ranges overlap on one end, or if the first range full falls within the second.
You will then need to test whether the second range is fully enclosed in the first, anything else will be covered in the first two tests. For that, you only need to test the start or the end.
Thus, I think this will work
$query = "SELECT u.first_name, u.last_name, u.avatar_filename, p.id, p.user_id, p.address, p.topic, p.latitude, p.longitude, d.name AS department_name
FROM user u
JOIN placement p ON p.user_id=u.id
JOIN department d ON d.id = u.department_id
WHERE p.active=1 AND (('{$dateA}' BETWEEN p.date_started AND p.date_finished) OR
('{$dateB}' BETWEEN p.date_started AND p.date_finished) OR
(p.date_started BETWEEN '{$dateA}' AND '{$dateB}'))";