1
votes

i need to make a php code for checking hotel room avaibility where user from the present day can book rooms upto 90 days or less and there are total 30 rooms available in the hotel,so if once i store the data for a user like his booking from one date till another next time if i want to check the avaibility how should i do it in php,what would be the logic.

obviously i simple query like this isn't correct for eg

$this->db->select('*')
    ->from('default_bookings')
    ->where('booking_from <',$input['fromdate'])
    ->where('booking_till >',$input['tilldate']);
3
I guess "form" should be "from"? - Raffael Luthiger
the problem isn't with the query, the problem is how would i know the rooms are available if i wanna check for date 1st-5th and 30 rooms are already booked from 1st-4th this will be true but that wont be right. - danish hashmi
What is your goal? Do you have a date-range (from>to) and the function should answer if there is some free space on a certain class of rooms, or do you want to list all available spaces without a concrete room-request? - Ron
yes the any user who comes to the site can select the date which will range from present day till 90 days and there are only 30 rooms in the hotel, so i need a function/query that to whom i give the two dates from -till i should can i allot a room out of the 30 rooms to him , is it available or not. - danish hashmi

3 Answers

1
votes

I assume, you want to find free space in you booking-calendar.

So you have a start-date and an end-date. You look in every room and make the following assumptions:

  1. The startdate and/or the enddate should not be present in an confirmed booking-period.
  2. There is no period between the start-date and the end-date.

A SQL-Query could look like this:

Table bookings {
    int booking_id
    int room_id
    datetime reservation_start
    datetime reservation_end
    ...
}

SELECT
    COUNT(*)
FROM
    bookings AS test1
WHERE
    room_id={$roomId}
    AND
    {$startdate} NOT BETWEEN reservation_start AND reservation_end
    AND
    {$enddate} NOT BETWEEN reservation_start AND reservation_end
    AND
    reservation_start NOT BETWEEN {$startdate} AND {$enddate};

Is the result is 0, then there is no intersection with existing reservations.

You can also automate the "looking into rooms" by using this query as a sub-query.

1
votes

This looks like Zend_DB_Select

Try this:

$this->db->select('*')
->form('default_bookings')
->where('booking_from < ?',$input['fromdate'])
->where('booking_till > ?',$input['tilldate']);

The "?" where missing then...

0
votes

This works for me. If the result is 0, then there is no booking in interval. The day of arrival can be same as day of departure.

        SELECT COUNT(*)
        FROM booking
        WHERE room_id = :room_id
        AND (
            (date_to > :start AND date_to < :end)
            OR
            (date_from > :start AND date_from < :end)
            OR
            (date_from < :start AND date_to > :end)
        )