Here is the script that I use to display the next 12 months (including the current one) and set a different style on the cells, depending on whether they are booked or not.
It seems long, but it is not difficult.
It queries the database, queries all the dates booked, adds them to the array, and then sets a different style when writing all the cells.
Tell me if this is not clear.
// Create an empty array, in which we will put all the booked dates from the database $bookedDates = array(); // Select all the existing bookings $sql = "SELECT * FROM reservation WHERE status = 'Confirmed'"; $result = mysql_query($sql); // For each reservation, we add all the days into the booking array while ($line = mysql_fetch_array($result)) { // Convert the SQL dates into mktime objects $splitStart = preg_split('/-/', $line['startDate']); $splitEnd = preg_split('/-/', $line['endDate']); $startDate = mktime(0, 0, 0, $splitStart[1], $splitStart[2], $splitStart[0]); $endDate = mktime(0, 0, 0, $splitEnd[1], $splitEnd[2], $splitEnd[0]); // We go through all the dates between the start date and end date of a specific reservation // to add them to the booking array $currentDate = $startDate; while ($currentDate < $endDate) { // We add the current day into the booked dates array $bookedDates[date('Ym-d', $currentDate)] = 'Booked'; $currentDate += 86400; //seconds in a day (which means 1 day) } } // The year and month of today (init) $currentYear = date('Y'); $currentMonth = date('m'); // We do 12 times the code in the while which writes 1 month, from 1 to 12 $monthIndex = 1; while ($monthIndex <= 12) { // Write the opening div of a calendar $resultString .= '<div class="oneCalendar">'; // We create a mktime object of the month we are writing $monthObject = mktime (0,0,0, $currentMonth, 1, $currentYear); $resultString .= '<table class="cal">'; $resultString .= '<caption>' . date('F Y', $monthObject) . '</caption>'; $resultString .= '<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>'; $dayIndex = 1; // Number of the day the month (the 1st) starts on (0 = Sun, 1 = Mon, etc.) $dayOffset = date('w', $monthObject); // Beginning of the first line $resultString .= '<tr>' . "\n"; for ($k = 0; $k < $dayOffset; $k++) $resultString .= '<td> </td>' . "\n"; // create an empty cell for every offset day for ($k = 0; $k < 7 - $dayOffset; $k++) // 7 - the day number that the month starts on (7 - 2 (Tuesday) = 5 which is Friday { $currentDate = $currentYear . '-' . $currentMonth . '-' . formatDay($k + 1); if (mktime(0, 0, 0, $currentMonth, $k + 1, $currentYear) < mktime(0, 0, 0, date('m'), date('d'), date('Y'))) $resultString .= '<td class="past" id="d'. $currentDate .'">' . ($k + 1) . '</td>' . "\n"; else if (isset($bookedDates[$currentDate]) && $bookedDates[$currentDate] == 'Booked') $resultString .= '<td class="booked" id="d'. $currentDate .'">' . ($k + 1) . '</td>' . "\n"; else $resultString .= '<td class="available" id="d'. $currentDate .'">' . ($k + 1) . '</td>' . "\n"; } $resultString .= '</tr>' . "\n"; // End of the first line // The remaining lines for ($i = 0; $i <= 4; $i++) // do all of the rows below 4 times { $resultString .= '<tr>' . "\n"; for ($j = 1; $j <= 7; $j++) //go through all 7 days { if ($dayIndex + (7 - $dayOffset) <= date('t', $monthObject)) { $currentDay = $dayIndex + (7 - $dayOffset); $currentDate = $currentYear . '-' . $currentMonth . '-' . formatDay($currentDay); if (mktime(0, 0, 0, $currentMonth, $currentDay, $currentYear) < mktime(0, 0, 0, date('m'), date('d'), date('Y'))) $resultString .= '<td class="past" id="d'. $currentDate .'">' . $currentDay . '</td>' . "\n"; else if (isset($bookedDates[$currentDate]) && $bookedDates[$currentDate] == 'Booked') $resultString .= '<td class="booked" id="d'. $currentDate .'">' . $currentDay . '</td>' . "\n"; else $resultString .= '<td class="available" id="d'. $currentDate .'">' . $currentDay . '</td>' . "\n"; } else { $resultString .= '<td> </td>' . "\n"; } $dayIndex++; } $resultString .= '</tr>' . "\n"; } $resultString .= '</table>'; $monthIndex++; $currentMonth++; if ($currentMonth > 12) { $currentYear++; $currentMonth = 1; } $currentMonth = formatDay($currentMonth); $resultString .= '</div>'; } return $resultString; } function formatDay($d) { if ($d < 10) return '0' . $d; else return $d; }`