I'm trying to make a calendar that shows which days are booked. I have made the following mysql table and php code, which shows all booked days between the first and last booked date. First is mysql table, 2nd is the function that creates the table, 3rd is the query and the call to the function. What I want to achieve is to show blocks of booked dates, not one block:
CREATE TABLE IF NOT EXISTS `bookings` (
`unid` mediumint(6) unsigned NOT NULL auto_increment,
`object_id` mediumint(4) unsigned NOT NULL,
`user` mediumint(6) unsigned NOT NULL,
`start` date NOT NULL,
`end` date NOT NULL,
`status` enum('0','1','2','3') NOT NULL default '0' COMMENT '1=reservation,2=paid',
PRIMARY KEY (`unid`),
KEY `start` (`start`,`end`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=100004 ;
--
-- Dumping data for table `bookings`
--
INSERT INTO `bookings` (`unid`, `object_id`, `user`, `start`, `end`, `status`) VALUES
(100001, 1001, 100001, '2009-12-21', '2009-12-25', '1'),
(100002, 1001, 120098, '2009-12-08', '2009-12-16', '0'),
(100003, 1001, 100001, '2009-12-30', '2009-12-31', '0');
function showCalendar($month,$year){
global $startday,$endday;
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
//This gets us the month name
$title = date('F', $first_day) ;
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ;
//Here we start building the table heads
echo "<table width=\"177\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"margin-bottom:15px; font-size:14px; text-align:center;border:1px solid #CCC; padding:4px;font-family: Arial, Helvetica, sans-serif;\">";
echo "<tr height=\"21\" style=\"background-color:#2a6ebb; color:#FFF\"><th colspan=7> $title $year </th></tr>";
echo "<tr height=\"21\" style=\"background-color:#ddedf9; color:#000\"><td width=14% class=\"calendarTop\">S</td><td width=14% class=\"calendarTop\">M</td><td width=14% class=\"calendarTop\">T</td><td width=14% class=\"calendarTop\">W</td><td width=14% class=\"calendarTop\">T</td><td width=14% class=\"calendarTop\">F</td><td width=14% class=\"calendarTop\">S</td></tr>";
//This counts the days in the week, up to 7
$day_count = 1;
echo "<tr height=\"24\">";
//first we take care of those blank days
while ( $blank > 0 )
{
echo "<td></td>";
$blank = $blank-1;
$day_count++;
}
//sets the first day of the month to 1
$day_num = 1;
//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
$pre = (($day_num == $startday) ? "class=\"highpre\"" : "");
$high = (($day_num > $startday && $day_num < $endday) ? "class=\"high\"" : "");
$post = (($day_num == $endday) ? "class=\"highpost\"" : "");
echo "<td ".$pre.$high.$post."> $day_num </td>";
$day_num++;
$day_count++;
//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr height=\"24\">";
$day_count = 1;
}
}
//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 )
{
echo "<td> </td>";
$day_count++;
}
echo "</tr></table>";
}
$start = "SELECT start,end FROM bookings WHERE object_id = '".$_GET['object']."'";
$query = @mysql_query($start);
$booked = array();
while ($row = @mysql_fetch_assoc($query))
{
$booked[] = $row;
}
// lookup first and last date in array
$min = PHP_INT_MAX;
$max = 0;
foreach ($booked as $i) {
$min = min($min, $i['start']);
$max = max($max, $i['end']);
}
$start = explode("-", $min);
$end = explode("-", $max);
$startday = $start[2];
$endday = $end[2];
showCalendar('12','2009');