Jump to content

consecutive dates


lional

Recommended Posts

Thanks to everyone that helped me with my calendar, I am making progress.
I would just like to know how one would write consecutive dates to a database. I want to have a from date and a to date. When the booking is made it must show up on my calendar.
What is the best way to write consecutive dates to a database so that my calendar can read it for eg if somebody books for 5 consecutive days, then my calendar must be able to show that particular room booked for those 5 days.
Could somebody please expalin to me how this is done

Regards

Lional
Link to comment
Share on other sites

try
[code]<?php
$room = 1;
$arrival = '2006-07-02';
$nights=5;

$arr_time = strtotime($arrival);

for ($i=0; $i<$nights; $i++) {
$booked_date = date('Y-m-d', strtotime("+$i days", $arr_time));
mysql_query("INSERT INTO bookings (room, booking_date)
VALUES ('$room', '$booked_date');") or die(mysql_error());
}
?>
[/code]
Link to comment
Share on other sites

  • 2 weeks later...
Thanks for the info Barand, it worked well for me
The last check that I need to make, is that for every new booking I would first like to check on the database if the room is not already booked for and of the requested days.
I have been trying my if statement but it is not working too well.
Regards

Lional
Link to comment
Share on other sites

I think you are going to need to check each date to make sure that the room is free...I would have thought you could modify Barand's code above to do this..
[code]
<?php
$room = 1;
$arrival = '2006-07-02';
$nights=5;

$arr_time = strtotime($arrival);

for ($i=0; $i<$nights; $i++) {
$booked_date = date('Y-m-d', strtotime("+$i days", $arr_time));
$sql = mysql_query("SELECT * FROM bookings WHERE room='$room' and booking_date='$bookingdate'");
$num = mysql_num_rows($sql);
if($num != "0"){
echo "The room is not available on those dates";
exit;
}
?>
[/code]

I would think that should work, but its untested.
Link to comment
Share on other sites

Thanks Gingerrobot.
It almost works, except if thjere is a booing for say 2006-10-27, 2006-10-28, and 2006-10-29 an I make a booking for 2006-10-28, 2006-10-29, and 2006-10-30, then it will accept the booking because the last dayis open. Am I doig something stupid

Thanks

Lional
Link to comment
Share on other sites

If you have a booking for (arrive) 2006-10-27 to (depart) 2006-10-29 then that is 2 nights so your records in the db will be
[pre]
room  booking_date
  1      2006-10-27
  1      2006-10-28
[/pre] 

If you now get a booking for 28th to 30th then you need to look for bookings for the nights of 28th and 29th
[code]<?php
$result = mysql_query("SELECT booking_date FROM bookings WHERE room = '$room'
AND booking_date BETWEEN  '2006-10-28' AND '2006-10-29' ");

if ($row = mysql_fetch_row($result) ) {
echo "Room booked on " ;
do (
echo $row[0] . ' ';
) while ($row = mysql_fetch_row($result) );
}[/code]

This should give "Room booked on 2006-10-28 "
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.