Prev | Current Page 409 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"


Figure 12-1. A grid calendar
Listing 12-1. Creating a Monthly Calendar
01 02 require_once 'Calendar/Month/Weekdays.php';
03
04 $month = new Calendar_Month_Weekdays(2006, 4, 0);
05
06 $month->build();
07
08 echo "\n";
09 echo "";
10 echo "
11 ";
12 while ($day = $month->fetch()) {
13 if ($day->isFirst()) {
14 echo "";
15 }
344 CHAPTER 12 ?–  D ATE AND TIME
16
17 if ($day->isEmpty()) {
18 echo "";
19 } else {
20 echo '";
21 }
22
23 if ($day->isLast()) {
24 echo "";
25 }
26 }
27
28 echo "
April, 2006
SuMoTuWeThFrSa
 '.$day->thisDay()."
";
29 ?>
Line 02: Because you want to build a grid calendar representing a month, the
Calendar_Month_Weekdays class is required. Line 02 makes this class available to
the script.
Line 04: The Calendar_Month_Weekdays class is instantiated, and the date is set to
April, 2006. The calendar should be laid out from Sunday to Saturday, so the third
parameter is set to 0, which is representative of the Sunday numerical offset (1 for
Monday, 2 for Tuesday, etc.).
Line 06: The build() method generates an array consisting of all dates found in
the month.
Line 12: A while loop begins, responsible for cycling through each day of the month.
Lines 13??“15: If $Day is the first day of the week, output a tag.


Pages:
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421