lional Posted July 2, 2006 Share Posted July 2, 2006 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 doneRegardsLional Quote Link to comment https://forums.phpfreaks.com/topic/13473-consecutive-dates/ Share on other sites More sharing options...
Barand Posted July 2, 2006 Share Posted July 2, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/13473-consecutive-dates/#findComment-52110 Share on other sites More sharing options...
lional Posted July 12, 2006 Author Share Posted July 12, 2006 Thanks for the info Barand, it worked well for meThe 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.RegardsLional Quote Link to comment https://forums.phpfreaks.com/topic/13473-consecutive-dates/#findComment-56630 Share on other sites More sharing options...
Daniel0 Posted July 12, 2006 Share Posted July 12, 2006 [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] Quote Link to comment https://forums.phpfreaks.com/topic/13473-consecutive-dates/#findComment-56631 Share on other sites More sharing options...
lional Posted July 12, 2006 Author Share Posted July 12, 2006 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 accountthankslional Quote Link to comment https://forums.phpfreaks.com/topic/13473-consecutive-dates/#findComment-56658 Share on other sites More sharing options...
GingerRobot Posted July 12, 2006 Share Posted July 12, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/13473-consecutive-dates/#findComment-56661 Share on other sites More sharing options...
lional Posted July 12, 2006 Author Share Posted July 12, 2006 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 stupidThanksLional Quote Link to comment https://forums.phpfreaks.com/topic/13473-consecutive-dates/#findComment-56693 Share on other sites More sharing options...
Barand Posted July 12, 2006 Share Posted July 12, 2006 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 " Quote Link to comment https://forums.phpfreaks.com/topic/13473-consecutive-dates/#findComment-56903 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.