Frank Schneider Posted Thursday at 02:57 PM Posted Thursday at 02:57 PM (edited) I'm currently struggling to find a PHP code to check room occupancy in a template. Based on a date range, I need to check if a room is already booked. Does anyone have a working code? folgendes reicht nicht: $selector.=",datum<=".$id->datum.",datumbis>=".$id->datumbis; Already booked: z.B. 13.08.2025 - 04.12.2025 Checks: 12.08.2025 - 01.10.2025 no problem problem: 01.07.2025 - 03.12.2025 problem: 01.10.2025 - 01.01.2026 Edited Thursday at 03:08 PM by Frank Schneider
cpx3 Posted 7 hours ago Posted 7 hours ago Gemini replied the following after examining things a little bit: The Logic of Date Overlaps The reason your current selector fails is that it only checks if the existing booking completely "swallows" your new request. To catch any overlap (partial or full), you need to use the following logic: A booking overlaps if: The End of the existing booking is after the Start of the new request. AND the Start of the existing booking is before the End of the new request. The Correct ProcessWire Selector Assuming $id->datum is your new Start and $id->datumbis is your new End: // Define your requested range $newStart = $id->datum; $newEnd = $id->datumbis; // Find any page that overlaps // Logic: Existing End > Requested Start AND Existing Start < Requested End $overlap = $pages->find("template=booking, datumbis>$newStart, datum<$newEnd"); if($overlap->count > 0) { // Room is occupied echo "This room is already booked by " . $overlap->count . " existing reservation(s)."; } else { // Room is available echo "The room is available for this period!"; } Why this solves your problems: Existing Booking: 13.08.2025 to 04.12.2025 Problem 1 (01.07.2025 – 03.12.2025): * Is existing end (04.12) > new start (01.07)? Yes. Is existing start (13.08) < new end (03.12)? Yes. Result: Overlap detected. Problem 2 (01.10.2025 – 01.01.2026): Is existing end (04.12) > new start (01.10)? Yes. Is existing start (13.08) < new end (01.01)? Yes. Result: Overlap detected. Pro-Tip: Changeover Days If your system allows a guest to check in on the same day someone else checks out (e.g., Check-out at 11:00, Check-in at 15:00), use strictly "greater than" (>) and "less than" (<). If you use >= or <=, the system will flag the changeover day as a conflict. Using strict operators allows the dates to touch without overlapping. Data Types Ensure that $newStart and $newEnd are in the same format as stored in the database (usually Unix Timestamps or YYYY-MM-DD). If you are using ProcessWire's native Date fields, comparing them as Timestamps is the most reliable method.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now