Calendar.php
The following listing shows the final calendar code. (Notice that the numbers at the beginning of each line are not part of the code — they appear here to aid in the discussion that follows the listing.)
10 11 12
20 21 22
/* Calendar.php
Display a calendar and allow user to move forward and backward through months via form controls.
$month = last month displayed $year = last year displayed
$calendar = TRUE if this script is calling, "mini" to display miniature calendar
Form control (POST) Next = Next month Form control (POST) Prev = Previous month
GET variable "display" overrides display - date to display passed in (m-y) format (e.g., June 2003 = "?display=6-2003")
// Globals for easy reference global $month, $year, $calendar;
// Get the variables from POST or GET function get_vars($type) {
60 61 62
global $month, $year, $calendar; // Get POST vars if ($type == "POST") { foreach($_POST as $key => $value) { $$key = $value;
// GET data is in different format for // simple calling, "display=m-y" // Put the mdy into array $dt $dt = explode("-",$_GET['display']); // If a valid date was passed, assign it to vars if (checkdate($dt[0],1,$dt[1])) { // Rearrange date to ISO 8601 format (yyyy-mm-dd) // and get timestamp ($ts) $ts = strtotime($dt[1]."-".$dt[0]."-1"); $month = date('n',$ts); $year = date('Y',$ts); } else {
// Not a valid date, use today $month = date('n'); $year = date('Y');
// Get calendar value if passed if (isset($_GET['calendar'])) { $calendar = $_GET['calendar']; } else { // Set to default $calendar = "TRUE";
// Set up the date to display function set_date() {
global $month, $year, $calendar;
// If GET n/v pairs exist, they override POSTS if (!empty($_GET)) { get_vars("GET");
// If $calendar is set then we can assume that // this call is under control of this script // (or a well-behaved handler) if (!isset($_POST["calendar"])) {
85. // If Next, then move month forward
86. if ($_POST["Next"] == ">>") {
94. // If Prev, then move month backward
95. if ($_POST["Prev"] == "<<") {
105.
106.
107. // Display the calendar form
108. function display_cal($month,$year,$calendar) {
109.
110. // Determine today to mark it accordingly
112.
113. // Build calls to script, include calendar var if
117. $reset_script = $this_script."?calendar=".$calendar;
119. $reset_script = $this_script;
122. // Get timestamp for first day of current month/year
123. $timestamp = strtotime($year."-".$month."-01");
160 161 162
$lastday = date('t',$timestamp); // What weekday does the first fall on? $wkday = date('w',$timestamp);
// Set monthtext and dow display according to size // of calendar if ($calendar != "mini") { // Not mini-cal, use full text $tblsize = 100;
$dow = array("Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday"); $monthtext = date('F',$timestamp); } else {
// Mini-cal, use single char days and 3 char month $tblsize = 20;
$dow = array("S","M","T","W","T","F","S");
// Print calendar header info print <<<HTML
<title>Calendar</title> <body>
<form action="$this_script" method="post"> <!-- Script control vars -->
<input type="hidden" name="calendar" value="$calendar"> <input type="hidden" name="month" value="$month "> <input type="hidden" name="year" value="$year">
<!-- Month header with Prev/Next buttons -- >
<table border=1>
<td colspan="2" align="left">
<input type="submit" name="Prev" value="<<"> </td>
<td colspan="3" align="center"> <strong>
<a href="$reset_script".$getdata>$monthtext $year</a> </strong> </td>
<td colspan="2" align="right">
<input type="submit" name="Next" value=">>"> </td> </tr>
175. <td width="$tblsize"><center><b>$dow[0]</b></center></td>
176. <td width="$tblsize"><center><b>$dow[1]</b></center></td>
177. <td width="$tblsize"><center><b>$dow[2]</b></center></td>
178. <td width="$tblsize"><center><b>$dow[3]</b></center></td>
179. <td width="$tblsize"><center><b>$dow[4]</b></center></td>
180. <td width="$tblsize"><center><b>$dow[5]</b></center></td>
181. <td width="$tblsize"><center><b>$dow[6]</b></center></td>
184.
185. HTML;
186.
187. // Skip days of week up to first day of month
190. print "\t<td align=\"right\" valign=\"top\" height=\"$tblsize\">";
194.
195. // Step through month, 1st through last day (28, 30, or 31)
196. // Print table cell for each
198. // Close row after each Sat, start next row with Sunday (0)
203. print "\t<td align=\"right\" valign=\"top\" height=\"$tblsize\">";
204. // If this is today, highlight it with red font
205. if ($today == $month."-".$x."-".$year) {
206. print "font color=\"#ff0000\">"; }
208. if ($today == $month."-".$x."-".$year) {
213.
216. print "\t<td align=\"right\" valign=\"top\" height=\"$tblsize\"> </td>\n";
219.
220. // Close open tags and page
224.
226.
228. HTML;
229.
231.
232.
233. // Main program body
234. // Set the dates to display
236. // Display the calendar
237. display_cal($month,$year,$calendar);
238.
Figure 26-2 shows the normal calendar. Figure 26-3 shows a sample of the calendar in mini-mode.
|
VP |
. a | |||||||||
|
tt Q, SM»*>> Iwi !<•. HMF | ||||||||||
|
Bttk |
^1 J i#yW4»m»tiii>i|i«p |
B[* |
m |
"»[ > |
-IE | |||||
|
L | ||||||||||
|
iSltMi | ||||||||||
|
Sndijr |
MoniUy Ttntfiy |
WMifvtay |
Tliorvlay |
Fridnv | ||||||
|
1 |
2 |
5 |
7 | |||||||
|
* |
> |
10 |
11 |
12 |
\l | |||||
|
15 |
17 |
It |
19 |
ID |
21 | |||||
|
2i |
u |
25 |
27 |
X | ||||||
|
.3. .J. t |
tocwfm 0 |
I'll -'W tftJP | ||||||||
- Figure 26-2 Sample of the calendar script output
Figure 26-3 Sample of the calendar script output in mini-mode
Figure 26-3 Sample of the calendar script output in mini-mode
Post a comment