Page 2 of 2

Posted: Mon Sep 19, 2005 6:50 am
by Jenk
Simple year planner I use (and made), which can be modified to extract from DB if needed and you can also use CSS to present in whatever style you want. To use, just pass which year you want to display via $_GET['y'] variable (e.g. calendar.php?y=2005)

If $_GET['y'] is not passed, it will display the current year.

Code: Select all

<?php

$year = (isset($_GET['y'])) ? intval($_GET['y']) : date("Y", time());

for ($month = 1; $month <= 12; $month++) {

	$ts_month = mktime(0,0,0,$month,1,$year);

	$numDaysMonth = date('t', $ts_month);

	for ($i = 1; $i <= $numDaysMonth; $i++) {

		$months[$month][$i] = getdate(mktime(0, 0, 0, $month, $i, $year));

	}

}

?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Year Planner</title>
</head>
<body>
<table>
<tr class="head">
<td><b><?php echo $year; ?></b></td><td>sun</td><td>mon</td><td>tue</td><td>wed</td><td>thur</td><td>fri</td><td>sat</td>
<td>sun</td><td>mon</td><td>tue</td><td>wed</td><td>thur</td><td>fri</td><td>sat</td>
<td>sun</td><td>mon</td><td>tue</td><td>wed</td><td>thur</td><td>fri</td><td>sat</td>
<td>sun</td><td>mon</td><td>tue</td><td>wed</td><td>thur</td><td>fri</td><td>sat</td>
<td>sun</td><td>mon</td><td>tue</td><td>wed</td><td>thur</td><td>fri</td><td>sat</td>
<td>sun</td><td>mon</td>
</tr>
<tr>
<?php

foreach($months as $days) {

	echo "<td class=\"month\">".substr($days[1]['month'],0,3)."</td>";

	$gap = $days[1]['wday'];

	if ($gap != 0) {

		echo "<td colspan=\"$gap\"></td>";

	}

	foreach ($days as $day) {

		if ($day['wday'] == 0 || $day['wday'] ==6) {

			echo "<td class=\"wkend\">{$day['mday']}</td>\n";

		}
		else {

			echo "<td class=\"content\">{$day['mday']}</td>\n";

		}

	}

	echo "</tr><tr>";

}
?>
</tr>
</table>
</body>
</html>
For a monthly calendar, I found this following function

Code: Select all

<?php

function build_calendar($month,$year,$dateArray) {

     // Create array containing abbreviations of days of week.
     $daysOfWeek = array('Su','Mo','Tu','We','Th','Fr','Sa');

     // What is the first day of the month in question?
     $firstDayOfMonth = mktime(0,0,0,$month,1,$year);

     // How many days does this month contain?
     $numberDays = date('t',$firstDayOfMonth);

     // Retrieve some information about the first day of the
     // month in question.
     $dateComponents = getdate($firstDayOfMonth);

     // What is the name of the month in question?
     $monthName = $dateComponents['month'];

     // What is the index value (0-6) of the first day of the
     // month in question.
     $dayOfWeek = $dateComponents['wday'];

     // Create the table tag opener and day headers

     $calendar = "<table class='calendar'>\n";
     $calendar .= "<caption>$monthName, $year</caption>\n";
     $calendar .= "<tr>";

     // Create the calendar headers

     foreach($daysOfWeek as $day) {
          $calendar .= "<th class='header'>$day</th>\n";
     }

     // Create the rest of the calendar

     // Initiate the day counter, starting with the 1st.

     $currentDay = 1;

     $calendar .= "</tr>\n<tr>";

     // The variable $dayOfWeek is used to
     // ensure that the calendar
     // display consists of exactly 7 columns.

     if ($dayOfWeek > 0) {
          $calendar .= "<td colspan='$dayOfWeek'>&nbsp;</td>\n";
     }

     while ($currentDay <= $numberDays) {

          // Seventh column (Saturday) reached. Start a new row.

          if ($dayOfWeek == 7) {

               $dayOfWeek = 0;
               $calendar .= "</tr>\n<tr>";

          }

         // Is the $currentDay a member of $dateArray? If so,
         // the day should be linked.

         if (in_array($currentDay,$dateArray)) {

            $date = "$year-$month-$currentDay";

            $calendar .= "<td class='linkedday'>
                       <a href='blogs.php?date=$date'
                       class='calendarlink'>$currentDay</a></td>\n";

          // $currentDay is not a member of $dateArray.

          } else {

               $calendar .= "<td class='day'>$currentDay</td>\n";

          }

          // Increment counters

          $currentDay++;
          $dayOfWeek++;

     }

     // Complete the row of the last week in month, if necessary

     if ($dayOfWeek != 7) {

          $remainingDays = 7 - $dayOfWeek;
          $calendar .= "<td colspan='$remainingDays'>&nbsp;</td>\n";

     }

     $calendar .= "</table>\n";

     return $calendar;

}

?>
From the tutorials found here:
http://www.zend.com/zend/trick/tricks-Oct-2002.php