PHP Calendar Bookings

Hi,

I’ve got a simple calendar system for bookings, which takes the information from a MySQL database and returns a date in a different colour if the date has a booking. This works fine for a daily booking system I recently used, but now I need to extend it to take bookings for a longer period.

Basically, I need the calendar to check the start date of a booking, then the end date and complete the dates in between as booked. I’m not sure if the current calendar can do this as there may be a problem with the original coding when you get bookings that overlap months.

The Code is below, can anyone take a look and let me have some ideas about changing this code slightly to make it recognise two dates from the database and all dates between those two.

Thanks for any help you can give, Here’s the code:

<?php 
include 'dbconx.php' ; 
$hourdiff = "8";

// set up some variables to identify the month, date and year to display 
if(!$_GET['currYear']) { $currYear = date("Y"); } else { $currYear = $_GET['currYear']; } 
if(!$_GET['currMonth']) { $currMonth = date("n"); } else { $currMonth = $_GET['currMonth']; } 
if(!$_GET['currDay']) { $currDay = date("j",time() + ($hourdiff * 3600)) ; } else { $currDay = $_GET['currDay']; } 
?>
<?php 
// list of names for days and months 
$days = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); 

$months = array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 

// number of days in each month 
$totalDays = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 

// if leap year, modify $totaldays array appropriately
if (date("L", mktime(0,0,0,$currMonth,1,$currYear))) 
{ 
$totalDays[2] = 29; 
} 
?>
<?php 
// find out which day the first of the month falls on
$firstDayOfMonth = date("w", mktime(0,0,0,$currMonth,1,$currYear)); 
?>
<?php
// formulate the SQL query - this is where the query needs to be looked at and how to formulate the start and end dates
$query = "SELECT * FROM calendar WHERE date_from >= '" . $currYear . "-" . sprintf("%02d", $currMonth) . "-01' OR date_to <= '" . $currYear . "-" . sprintf("%02d", $currMonth) . "-" . $totalDays[$currMonth] . "'"; 

// run the query on the database 
$result = mysql_query($query) or die (mysql_error()); 

$x=0; 
$dateList=array(); 
if(mysql_num_rows($result) > 0) 
{ 
while($row = mysql_fetch_array($result)) 
{ 
$dates = explode("-", $row["date_from"]); 
$dateList[$x] = $dates[2]; 
$x++; 
$dates2 = explode("-", $row["date_to"]); 
$dateList2[$x2] = $dates2[2]; 
$x2++; 
} 
} 
?> 
<?php 
// set up variables to display previous and next months correctly 

// defaults for previous month 
$prevMonth = $currMonth-1; 
$prevYear = $currYear; 

// if January, decrement year and set month to December
if ($prevMonth < 1) { 
$prevMonth=12; 
$prevYear--; 
} 

// defaults for next month 
$nextMonth = $currMonth+1; 
$nextYear = $currYear; 

// if December, increment year and set month to January
if ($nextMonth > 12) { 
$nextMonth=1; 
$nextYear++; 
} 
?> 
<table align="center" cellpadding="2" cellspacing="5" style="border:1px #000000 solid"> 
<!-- month display --> 
<!-- this is the first row of the calendar, with links active --> 
<tr> <td><a href="calendar.php?currMonth=<?php echo $prevMonth; ?>&currYear=<?php echo $prevYear; ?>" class="feat"><font face="Arial" 
size="-2"><<</font></a></td> 

<td colspan="5" align="CENTER"><font face="Arial" size="-1"><b><?php echo $months[$currMonth] . " " . $currYear; ?></b></font></td> 

<td><a href="calendar.php?currMonth=<?php echo $nextMonth; ?>&currYear=<?php echo $nextYear; ?>" class="feat"><font face="Arial" 
size="-2">>></font></font></a></td> 
</tr> 
 

<!-- day names --> 
<tr> 
<?php 
for ($x=0; $x<7; $x++) 
{ 
echo "<td><font face=Arial size=-2>" . substr($days[$x],0,3) . 
"</font></td>"; 
} 
?> 
</tr> 

<!-- start displaying dates --> 
<tr> 
<?php 
// display blank spaces until the first day of the month
for ($x=1; $x<=$firstDayOfMonth; $x++) { 
// this comes in handy to find the end of each 7-day block 
$rowCount++; 
echo "<td><font face=Arial size=-2>&nbsp;</font></td>
"; }

// counter to track the current date 
$dayCount=1; 
while ($dayCount <= $totalDays[$currMonth]) { 
// use this to find out when the 7-day block is complete and display a new row 
if ($rowCount % 7 == 0) 
{ 
echo "</tr>
<tr>
"; 
} 

// if today, display in different colour 

// print date
if ($dayCount == date("j",time() + ($hourdiff * 3600)) && $currYear == date("Y") && $currMonth == 
date("n")) 
{ 
echo "<td align=center bgcolor=#DAEAC6><font face=Arial size=-1>" . $dayCount. "</font>"; 
} 

else 
{ 
echo "<td align=center" ;
for ($y=0; $y<sizeof($dateList); $y++) 
{ 
if ($dateList[$y] == $dayCount) 
{ 
echo " bgcolor=red"; 
}
}
for ($y2=0; $y2<sizeof($dateList2); $y2++) 
{ 
if ($dateList2[$y2] == $dayCount) 
{ 
echo " bgcolor=red"; 
}
}
echo "><font face=Arial size=-1>" . $dayCount . "</font>"; 
 
}

// newly-added code to find out is appointment is already scheduled 
// and print indicator if so 
 

echo "</td>
"; 

// increment counters 
$dayCount++; 
$rowCount++; 
} 
?>
</tr> 

<tr> 
<td align=right colspan=7> 
<font face=Arial size=-2> 
<a href="calendar.php" class="feat">This Month</a> </font> </td> </tr> 

</table>

The calendar script comes from a Tutorial, with the MySQL statements from me, as I needed to show when a date, or certain part of a day had a booking on it. There is no limit to the amount of time in advance you can book as far as I know

No matter, I have managed to work a code into it grabbing the first and last date of the booking, then querying the current date if it is between the two dates. It is an ugly script, but it works perfect. I’m sure someone with a lot more experience and a better coder than I am could have done it easier and better looking, but it works!

If anyone wants the code for something similar, just pm me