| Source of: calendar.php (Download Source) |
| Last Modified: Fri, 16 Feb 2007 13:38:18 UTC |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
/* This is a brief introduction on how to use the Calendar class. I will create * a Calendar displaying the year of the viewer's choice, let the viewer change * the look of the Calendar a bit and display some events. * Send any suggestions, questions and flames to <kyrael@web.de> */ // Include the calendar class require_once 'class.calendar.php'; /* Locale settings are used to change the way strftime() formats date and time, * it's in no way neccessary for the calendar but nice to have. Ignore this if * you're not interested in setting the locale but just want to know how to * customize the calendar. Btw, this doesn't work on windows machines, but it * does not harm. */ // Set the default locale $locale = "en_US"; // The array of allowed locales $locales = array( "en" => "en_US", "de" => "de_DE", "nl" => "nl_NL", "at" => "de_AT", "dk" => "da_DK", "es" => "es_ES", "it" => "it_IT", "fi" => "fi_FI", "fr" => "fr_FR", "no" => "no_NO", "pl" => "pl_PL", "sv" => "sv_SE" ); // if the viewer specified a language, change the locale if (isset($_GET['language']) && isset($locales[$_GET['language']])) { $locale = $locales[$_GET['language']]; } setlocale(LC_TIME, $locale); /* Now, let's start to use the class. I will explain every method of the class * I use, so just read on. */ // create an instance for the class $cal = new Calendar; // change layout options. if the viewer didn't chose to hide an option, he will // want to see it, so we just check the get parameters. That way no warnings // will be generated if the GET parameters are not there. $print_title = empty($_GET['hide_title']); $print_number_of_week = empty($_GET['hide_number_of_week']); $print_days_of_week = empty($_GET['hide_days_of_week']); /* Ok, now we will start using the objects methods. First method is "setLayout" * which is - surprise - used to alter the layout of the calendar. It takes an * array as an argument, and merges this array with the array of default * options and saves the result as the new default. A list of the array entries * and their default values follows: 'table_start' => '<table>', The tag that starts the table containing the calendar. 'table_end' => '</table>', Same, just for the end. 'row_start' => '<tr>', This tag starts every row. 'row_end' => '</tr>', And this tag ends it. 'cell_start' => '<td>', This tag starts the "date" cells (one for every day in the month). 'cell_end' => '</td>', End again... 'cell' => '%d', This is the content of the date cell. it gets parsed through strftime. see http://www.php.net/manual/en/function.strftime.php for a list of conversion specifiers. %d is the day of the month as a two-digit number (01 to 31). Note: if you want a literal %, you have to enter a %%. 'empty_cell_start' => '<td>', This starts empty cells (the ones that fill up the empty spaces at the beginning/end of a month). 'empty_cell_end' => '</td>', I bet you can guess it by now, yes? 'empty_cell' => ' ', What is used to fill empty cells. Note: If this is empty, some browser won't display the cell at all. 'week_cell_start' => '<td>', This defines how the cells displaying the week number of the year start. 'week_cell_end' => '</td>', Do i have to say anything? 'week_cell' => CALENDAR_WEEK_FORMAT, CALENDAR_WEEK_FORMAT is a constant that gets defined when the file containing the calendar class gets executed. It contains %V if available (the week number of the year according to ISO 8601:1988), else it contains %W (week number of the year, starting with monday as the first day of the first week). 'corner_cell_start' => '<td>', Starts the cell in the upperleft corner, if both week numbers and days of week get displayed. 'corner_cell_end' => '</td>', I'll just leave this empty. 'corner_cell' => ' ', Same as empty_cell. 'head_cell_start' => '<td>', Starts the cell containing the days of the week 'head_cell_end' => '</td>', 'head_cell' => '%s', How the cells containing the days of the week get displayed... this string is the first argument of a call to sprintf (http://www.php.net/manual/en/function.sprintf.php) with the second argument being the day of the week specified in another layout option ('day_of_week_names'). 'title_cell_start' => '<td %colspan%>', This starts the cell that is the "title", spanning all columns of the table. 'title_cell_end' => '</td>', 'title_cell' => '%B %Y', The format of the content of the title. %B %Y will print something like "November 2002" ... see strftime for more options. 'day_of_week_names' => array('Mon','Tue','Wed','Thu','Fri','Sat', 'Sun'), The days of the Week. The class provides a method to get them according to the current locale(Calendar::getWeekDays), we'll get to that later. 'print_days_of_week' => true, Defines if the days of the week get included in the calendar. 'print_title' => true, Defines if the title gets included in the calendar. 'print_number_of_week' => true Defines if the week numbers get included in the calendar. * Options that aren't mentioned won't change. */ $cal->setLayout(array( 'table_start' => '<table class="calendar_table">', 'cell_start' => '<td class="calendar_cell">', 'empty_cell_start' => '<td class="calendar_empty">', 'week_cell_start' => '<td class="calendar_week">', 'title_cell_start' => '<td class="calendar_title" %colspan%>', 'corner_cell_start' => '<td class="calendar_corner">', 'head_cell_start' => '<td class="calendar_head">', 'print_number_of_week' => $print_number_of_week, 'print_days_of_week' => $print_days_of_week, 'print_title' => $print_title, 'day_of_week_names' => $cal->getWeekDays('%a') )); /* Notice the call to $cal->getWeekDays('%a'), it's the method i mentioned * earlier to get the weekdays according to your current locale. It's again * a format used with strftime, this time %A (full name of the day of the week) * and %a (same but abbreviated) are the only specifiers that make sense though. */ /* Next two commands are calls to the method Calendar::addDate, which is used * to highlight special days. First argument is a string that gets passed to * strtotime() to get the unix-timestamp. Dates will only be recognized if they * belong to 0:00 (midnight) of the day. strtotime() can handle a variety of * date/time formats, for example "2002-06-30", "6 jun 2002", "June 6, 2002", * "06/30/02" all specify the same date. For more information see: * http://www.php.net/manual/en/function.strtotime.php * http://www.gnu.org/manual/tar-1.12/html_chapter/tar_7.html * * Second argument is a layout array as explained above. */ $cal->addDate('2002-06-30', array('cell_start' => '<td class="calendar_cell" style="background-color: #ff6666;">', 'cell' => '<span style="font-weight: bold;">%d</span>') ); /* This command makes use of the possibility to specify relative times. It * causes the current day to be highlighted in blue, with the number being bold. * Another possibility would be 'next Thursday', '0:00 today + 1 day' or alike. */ $cal->addDate('0:00 today', array('cell_start' => '<td class="calendar_cell" style="background-color: #ddffff;">', 'cell' => '<span style="font-weight: bold;">%d</span>') ); /* The next command is a call to Calendar::addInterval, a method used to * highlight a period of time in the Calendar. The first argument tells when the * interval starts, the second argument tells when it ends. The format is the * same as explained above, except that it doesn't have to be 0:00. A day will * be highlighted if it's beginning (0:00) is in the interval. Third argument * is a layout array. */ $cal->addInterval('2002-05-31', '2002-06-30', array('cell_start' => '<td class="calendar_cell" style="background-color: #ffdddd;">') ); /* Next two commands are calls to Calendar::addPeriodic, a method used to * highlight events happening in a special patterns. The first and second * arguments specify the pattern that is looked for. The first argument gets * feed to strftime(), along with the unix timestamp of the day that is being * processed. If the return value of strftime() matches the second argument * given, the day will be highlighed. Third argument specifies the format, * as usual. Two examples follow: */ /* First example: * This looks for a special day in the year (my birthday ;)). The format is * '%m-%d', meaning 'month-day', both in two digits. The match is '11-12', * the 12th november. */ $cal->addPeriodic('%m-%d', '11-12', array('cell_start' => '<td class="calendar_cell" style="background-color: #aaaaff;">', 'cell' => '<a href="#" onclick="alert(\'Birthday of et!\');">%d</a>') ); /* Second example: * This highlights every sunday. The format is '%w', which is the day of the * week as a number from 0 to 6 with 0 being sunday, and the match is, i bet * you guessed it, 0. */ $cal->addPeriodic('%w', '0', array('cell_start' => '<td class="calendar_cell" style="background-color: #dedeff;">') ); /* Some HTML code follows, nothing of interest to understand how the class * works, but i don't want it to look too dull ;) */ print <<<HTML <html> <head> <title>Calendar</title> <style type="text/css"> a { color: black; } td { text-align:center; vertical-align:top; } .calendar_table { background-color:#eeeeee; border: 1px solid black; } .calendar_empty { background-color:#eeeef3; border: 1px solid #7f7f7f; width: 30px; } .calendar_cell { background-color:#f0f0ff; border: 1px solid #333333; width: 30px; } .calendar_week { background-color:#9090A0; border: 1px solid black; width: 30px; } .calendar_corner { background-color:#aaaabe; border: 1px solid #7f7f8f; width: 30px; } .calendar_head { background-color:#9090A0; border: 1px solid black; width: 30px; } .calendar_title { font-weight: bold; background-color:#9090A0; border: 1px solid black; } </style> <body> HTML; /* Fetch the year from the URL, if it exists, and prevent failures * (unix-timestamp only range from 1970-01-01 01:00 to somewhere in january, * 2038) */ $year = isset($_GET['year'])?(int)$_GET['year']%10000:strftime('%Y'); if ($year < 1971 || $year > 2037) { $year = strftime('%Y'); } /* Finally, display the calendars. This is a loop that displays the Calendar * in a neat table with every quartal in one row. Calendar::display() is called * here, first argument is the year, second argument is the month that the * calendar shall be displayed for. Calendar::fetch() also exists, this method * returns the Calendar rather than displaying it, the arguments are the same. */ print '<table style="border: 1px solid black; background-color:#333366; padding:5px;">'; for ($i = 0; $i < 12; $i++) { if ($i%3 == 0) print '<tr>'; print '<td>'; $cal->display($year, $i+1); print '</td>'; if ($i%3 == 2) print '</tr>'; } print "</table>"; /* We are done here, all that follows is HTML code to let the viewer play a * little with this example, and look at this source. */ $check1 = $print_title?'':'checked="checked"'; $check2 = $print_number_of_week?'':'checked="checked"'; $check3 = $print_days_of_week?'':'checked="checked"'; print <<<HTML <form action="{$_SERVER['PHP_SELF']}" method="GET"><div> <select name="language"> <option value="en"> English </option> <option value="de"> Deutsch </option> <option value="at"> Österreichisch </option> <option value="nl"> Dutch </option> <option value="dk"> Dansk </option> <option value="es"> Español </option> <option value="fi"> Suomi </option> <option value="sv"> Sverige </option> <option value="fr"> Français </option> <option value="it"> Italiano </option> <option value="no"> Norsk </option> <option value="pl"> Polish </option> </select> Select language <br /> <input type="checkbox" name="hide_title" value="checked" {$check1} /> Hide header containing month and year<br /> <input type="checkbox" name="hide_number_of_week" value="checked" {$check2} /> Hide column with week numbers<br /> <input type="checkbox" name="hide_days_of_week" value="checked" {$check3} /> Hide the names of the weekdays<br /> <input type="text" maxlength="4" size="4" name="year" value="{$year}" /> The year that the calendar is to be created for (1971-2037)<br /> <input type="checkbox" name="show_source" value="checked"/> Show the source at the bottom of the page <br /> <input type="submit" value="Create Calendar" /> </div></form> HTML; if (!empty($_GET['show_source'])) { print "<br /><br />Source:<hr />"; show_source(__FILE__); print "<br /><br />Source of class.calendar.php:<hr />"; show_source("class.calendar.php"); } print <<<HTML </body> </html> HTML; |