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
https://forums.phpfreaks.com/topic/13473-consecutive-dates/
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
https://forums.phpfreaks.com/topic/13473-consecutive-dates/#findComment-52110
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
https://forums.phpfreaks.com/topic/13473-consecutive-dates/#findComment-56630
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
https://forums.phpfreaks.com/topic/13473-consecutive-dates/#findComment-56661
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
https://forums.phpfreaks.com/topic/13473-consecutive-dates/#findComment-56903
Share on other sites

Archived

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

×
×
  • 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.