Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the script I use to display the 12 next months (including the current one) and set a different style on cells depending on if they are booked or not.</p> <p>It seems long but it's not complicated.</p> <p>It queries the DB, asking for all the booked dates, adds them into a Array, and then set a different style accordingly while writing all the cells.</p> <p>Say if it's not clear.</p> <p> <pre><code> // 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 &lt; $endDate) { // We add the current day into the booked dates array $bookedDates[date('Y-m-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 &lt;= 12) { // Write the opening div of a calendar $resultString .= '&lt;div class="oneCalendar"&gt;'; // We create a mktime object of the month we are writing $monthObject = mktime (0,0,0, $currentMonth, 1, $currentYear); $resultString .= '&lt;table class="cal"&gt;'; $resultString .= '&lt;caption&gt;' . date('F Y', $monthObject) . '&lt;/caption&gt;'; $resultString .= '&lt;tr&gt;&lt;th&gt;Sun&lt;/th&gt;&lt;th&gt;Mon&lt;/th&gt;&lt;th&gt;Tue&lt;/th&gt;&lt;th&gt;Wed&lt;/th&gt;&lt;th&gt;Thu&lt;/th&gt;&lt;th&gt;Fri&lt;/th&gt;&lt;th&gt;Sat&lt;/th&gt;&lt;/tr&gt;'; $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 .= '&lt;tr&gt;' . "\n"; for ($k = 0; $k &lt; $dayOffset; $k++) $resultString .= '&lt;td&gt;&amp;nbsp;&lt;/td&gt;' . "\n"; // create an empty cell for every offset day for ($k = 0; $k &lt; 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) &lt; mktime(0, 0, 0, date('m'), date('d'), date('Y'))) $resultString .= '&lt;td class="past" id="d'. $currentDate .'"&gt;' . ($k + 1) . '&lt;/td&gt;' . "\n"; else if (isset($bookedDates[$currentDate]) &amp;&amp; $bookedDates[$currentDate] == 'Booked') $resultString .= '&lt;td class="booked" id="d'. $currentDate .'"&gt;' . ($k + 1) . '&lt;/td&gt;' . "\n"; else $resultString .= '&lt;td class="available" id="d'. $currentDate .'"&gt;' . ($k + 1) . '&lt;/td&gt;' . "\n"; } $resultString .= '&lt;/tr&gt;' . "\n"; // End of the first line // The remaining lines for ($i = 0; $i &lt;= 4; $i++) // do all of the rows below 4 times { $resultString .= '&lt;tr&gt;' . "\n"; for ($j = 1; $j &lt;= 7; $j++) //go through all 7 days { if ($dayIndex + (7 - $dayOffset) &lt;= date('t', $monthObject)) { $currentDay = $dayIndex + (7 - $dayOffset); $currentDate = $currentYear . '-' . $currentMonth . '-' . formatDay($currentDay); if (mktime(0, 0, 0, $currentMonth, $currentDay, $currentYear) &lt; mktime(0, 0, 0, date('m'), date('d'), date('Y'))) $resultString .= '&lt;td class="past" id="d'. $currentDate .'"&gt;' . $currentDay . '&lt;/td&gt;' . "\n"; else if (isset($bookedDates[$currentDate]) &amp;&amp; $bookedDates[$currentDate] == 'Booked') $resultString .= '&lt;td class="booked" id="d'. $currentDate .'"&gt;' . $currentDay . '&lt;/td&gt;' . "\n"; else $resultString .= '&lt;td class="available" id="d'. $currentDate .'"&gt;' . $currentDay . '&lt;/td&gt;' . "\n"; } else { $resultString .= '&lt;td&gt;&amp;nbsp;&lt;/td&gt;' . "\n"; } $dayIndex++; } $resultString .= '&lt;/tr&gt;' . "\n"; } $resultString .= '&lt;/table&gt;'; $monthIndex++; $currentMonth++; if ($currentMonth &gt; 12) { $currentYear++; $currentMonth = 1; } $currentMonth = formatDay($currentMonth); $resultString .= '&lt;/div&gt;'; } return $resultString; } function formatDay($d) { if ($d &lt; 10) return '0' . $d; else return $d; }` </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload