PHP Calendar [] Construction

This prints the dates of the current month in box format, centered on a page. The current month, or the next, will be shown, with certain dates highlighted as anchors. The original code is from the contributed notes by joey.garcia at usa.net, 13-Nov-2000, at SourceForge.

The PHP code below is in use at [http://chicagoart.net/calendar.php]

... to list art openings in Chicago. Clicking on any highlighted date brings up a page of events for that day.

Just name the file below 'calendar.php'

The changes from Garcia's code are that a month and year do not have to be entered -- the code below assumes that only the current month and the next are of interest. Additionally, the cell format has been adjusted to allow use of the Lynx browser (which we use to administer the site), dashes have been added after the closing date, and the number of weeks has been correctly adjusted. Sample:

ChicagoArt.Net

Chicago individual gallery announcements.

Events for August 2003

there are 9 events posted
SunMon TueWedThuFriSat
--  --  --  --  --   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  --  --  --  --  --  -- 
not all events may be listed yet

NB: I have deleted all the direct use of HTML tags and text copy in the following, and have only shown a centered TABLE. Time would be saved by not depending exclusively on PHP 'print()'.

The values of $month is 1 for next month, and 0 for the current month. $thispage and $otherpage can be used in the TITLE, or used with H1 title tags, or as a link to the the next (or current) month.



 <?php

 if ($month == "" | $month < 1) {
        $month = 0; $othermonth = 1; }
        else {  $month = 1; $othermonth = 0; } 
 if ($month == 0) { 
	$thispage = "this month"; $otherpage = "next month"; }
	else { $thispage = "next month"; $otherpage = "this month"; }


The following prefigures the month name, year, and month number for the current or the following month before display. The next month depends on values for the current month, to allow getting past new year gracefully.



 $date_time_array = getdate(time());
	$this_month = $date_time_array["month"];
	$this_year  = $date_time_array["year"];
	$this_mon   = $date_time_array["mon"];

 if ($month == 1) {
 $next_month_stamp = mktime(1,1,1,$this_mon + 1,1,$this_year);
 $date_time_array = getdate( $next_month_stamp );
	$this_month = $date_time_array["month"];
	$this_year  = $date_time_array["year"];
	$this_mon   = $date_time_array["mon"];
 }
	// title and link to the other calendar month.
 print "<center> <h1>$this_month $this_year</h1> Go to
 <a href=\"calendar.php?month=$othermonth\">[$otherpage]</a>";

	// total entries in the array.
 $howmany = count($events) ;
 print "there are $howmany event(s) posted";

	// find what weekday the first day of the month falls on.
	// figure weekday _number_ for first day of month
	// wday is zero indexed, prog below uses 1 indexing
 $first_day_stamp = mktime(0,0,0,$this_mon,1,$this_year);
 $date_time_array = getdate( $first_day_stamp );
 $firstwkday = $date_time_array["wday"] + 1;

	// find the total days of the month.
	// figure last day of a month as:
	// last day of this month = day zero of next month 
 $last_day_stamp = mktime(0,0,0,$this_mon + 1,0,$this_year);
 $date_time_array = getdate( $last_day_stamp );
 $daysinmonth = $date_time_array["mday"] ;

 print "<P><table width=80% align=center border=1>
 <tr>
 <th>Sun</th><th>Mon</th>
 <th>Tue</th><th>Wed</th>
 <th>Thu</th><th>Fri</th>
 <th>Sat</th>
 </tr>";

 $date=1;             
	// months are either 5 or 6 weeks,
	// but 3 weeks if Apr 1 is a Sunday
	// (doesn't happen often).
 $maxwk = 5;
 if (($firstwkday + $daysinmonth -1) > 35) { $maxwk = 6 ;}
 for ($week=1; $week <= $maxwk; $week++) {
	// the newlines are just for page formatting
   echo "<tr>\n";
   for($wkday=1; $wkday <=7; $wkday++) {
     if( ($week>1) || ($wkday>=$firstwkday) ){
       print "<td height=40px align=\"center\">";
       if ($date <= $daysinmonth) {
         if ($date < 10) {
           print " ";
	 } 

Insert links here for events from array $events -- a list of events in a format of simple day-of-the-month numbers. The link will link to "eventlist.php" with {whatever} appended variables. $month = 0|1 can be used to differentiate between the two months.



        if (in_array("$date", $events)) { 
 print "<a href=\"eventlist.php?var1=$date&var2=$month\">$date</a>";
          }     
        else {      
           print $date;
        }     
         echo " ";
         $date++;
       }       
       else {            
         echo "-- ";
       }                
       echo "</td>\n";
     }           
     else {              
       print "<td height=40px align=\"center\">-- ";
       echo "</td>\n";
     }                
   }         
   echo "</tr>\n";
 }           
 print "</table></center>";
 ?>


COPYRIGHT NOTICES:

Copyright (C) 2002 Jno Cook, Aesthetic Investigation, Chicago

jno (at) blight (dot) com, http://jnocook.net/

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. http://www.gnu.org/copyleft/gpl.html


[logo]


Website Provider: Outflux.net, www.Outflux.net
URL:http://jnocook.net/geek/calendar.htm