Home  >  Q&A  >  body text

Search between two date ranges

I am developing a car rental system where people can book a car in this page.

So I need to know how to search using the customer's date range (rent and return) from the booking table.

I have this table:

CREATE TABLE `autos_bookings` (
  `id` int(11) NOT NULL,
  `booking_id` int(11) NOT NULL DEFAULT '0',
  `auto_id` int(11) NOT NULL DEFAULT '0',
  `date_out` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `date_return` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `autos_bookings`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `autos_bookings`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

So, what I want to do is have another date range (for example, let's say: $dateclientewantsrent and $dateclientewantsreturn) and search that table to see if there are any existing Booking.

SELECT * FROM `autos_bookings` WHERE SOMETHING

P粉884548619P粉884548619181 days ago425

reply all(1)I'll reply

  • P粉118698740

    P粉1186987402024-04-04 10:41:43

    Two ranges overlap when each range start point is smaller than the opposite range end point. Right now

    ...
    WHERE booking.date_out < $date_cliente_wants_rent
      AND $date_cliente_wants_return < booking.date_return
    ...

    Depending on the logic, one or both comparisons may be weak.

    reply
    0
  • Cancelreply