Jump to content


Photo

consecutive dates


  • Please log in to reply
7 replies to this topic

#1 lional

lional
  • Members
  • PipPipPip
  • Advanced Member
  • 266 posts

Posted 02 July 2006 - 05:29 PM

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

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 02 July 2006 - 06:22 PM

try
<?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());
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 lional

lional
  • Members
  • PipPipPip
  • Advanced Member
  • 266 posts

Posted 12 July 2006 - 07:29 AM

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

#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 12 July 2006 - 07:31 AM

<?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
}
?>


#5 lional

lional
  • Members
  • PipPipPip
  • Advanced Member
  • 266 posts

Posted 12 July 2006 - 09:15 AM

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

#6 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 12 July 2006 - 09:21 AM

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..
<?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;
}
?>

I would think that should work, but its untested.

#7 lional

lional
  • Members
  • PipPipPip
  • Advanced Member
  • 266 posts

Posted 12 July 2006 - 10:28 AM

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

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 12 July 2006 - 07:12 PM

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
<?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) );
}

This should give "Room booked on 2006-10-28 "
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users