Jump to content

Archived

This topic is now archived and is closed to further replies.

lional

consecutive dates

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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[code]<?php
//...
$query = mysql_query("SELECT * FROM bookings WHERE booking_date='{$requested_date}'");
if(mysql_num_rows($query) > 0)
{
echo "Sorry, that date is already booked";
}
else {
// do stuff
}
?>[/code]

Share this post


Link to post
Share on other sites
Barend showed me how to update the database on July 02.
Will your method stll do this or will it only take one day into account

thanks

lional

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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 "

Share this post


Link to post
Share on other sites

×

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.