John Kariuki
John Kariuki

Reputation: 5724

Php/mysql check number of booked rooms

I'm having a small issue regarding on how to check if a room is booked or not.

So basically I a have a table as shown below

Accomm

  1. accomm_id
  2. checkin [stored as timestamp]
  3. checkout [stored as timestamp]

What I have so far, is that if the user inputs a checkin and checkout date which are both converted to timestamps. That part is done.

So what i have failed to figure out is the following:

Please assist or guide me on this one. Thanks.

$booked_rooms = 0;
while($accommodation_package = mysql_fetch_array($query)) {

    // $this->checkin, $this->checkout are values fed by the user
    // $accommodation_package['checkin/out'] are the values in the database
    if($this->checkin >= $accommodation_package['checkin'] && $this->checkout <= $accommodation_package['checkout']) {
        $booked_rooms +=1;
    }
}
echo $booked_rooms;

Well I thought the above would work but when checkin and checkout dates from the client are on different months it does not work.

Upvotes: 0

Views: 781

Answers (2)

madhippie
madhippie

Reputation: 158

Assuming that you have stored our check-in and check-out dates in the format 'd-m-Y' >ou could execute the following SQL statement:

SELECT COUNT(acomm_id) FROM Accomm WHERE checkin <= yourDateString AND checkout => yourDateString

Hope this helps.

Upvotes: 1

prava
prava

Reputation: 3986

Try with this:

<?php
$currentTime = date('Y-m-d H:i:s');

$sql = "SELECT COUNT(accomm_id)
        FROM accom
        WHERE checkin <= $currentTime
        AND checkout >= $currentTime";
$query = mysqli_query($sql);
// code to get the details.

Upvotes: 0

Related Questions