Pages

Friday, January 13, 2012

Simple Calendar

Set-up the variables:
<?php
  $monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 

  if (!isset($_REQUEST["year"])) { 
    $_REQUEST["year"]  = date("Y"); 
  }
  
  $cYear  = $_REQUEST["year"];
               
  $prev_year = $cYear - 1;  
  $next_year = $cYear + 1;

  // list the holidays
  $holidays = array(
    array(1,23),    // jan
    array(),        // feb
    array(),        // march
    array(5,6,9),   // apr
    array(1),       // may
    array(12),      // june
    array(),        // july
    array(21,27),   // aug
    array(),        // sept
    array(),        // oct
    array(1,2,30),  // nov
    array(25,30,31) // dec
  );
?>

Create the calendar:
<div style="text-align:center;">
   <table style="width:848px; margin-left:auto; margin-right:auto;">
   <tr>
      <td style="text-align:left; width:33.3%">
         <a href="<?php echo $_SERVER["PHP_SELF"] . "?year=" . $prev_year; ?>">Previous</a>
      </td>
      <td style="text-align:center; width:33.4%">
         <strong><?php echo $cYear; ?></strong>
      </td>
      <td style="text-align:right; width:33.3%">
         <a href="<?php echo $_SERVER["PHP_SELF"] . "?year=" . $next_year; ?>">Next</a>
      </td>
   </tr>
   </table>   
   
   <div style="width:848px; height:696px; margin-left:auto; margin-right:auto;">   
   <?php  
      $ctr = 1;
     
      foreach($monthNames as $monthName)
      {
   ?>
         <div id="calendar_div" name="calendar_div" style="float:left; border:solid 1px #000; height:230px;">
         <table style="width:210px;">
            <tr>
               <td align="center">
                 <table width="100%" border="0" cellpadding="2" cellspacing="2">
                  <tr align="center">
                    <td colspan="7" style="color:#fff; background-color:#999;"><strong><?php echo $monthName; ?></strong></td>
                  </tr>
                  <tr>
                    <td style="color:#fff; background-color:#999; text-align:center;"><strong>S</strong></td>
                    <td style="color:#fff; background-color:#999; text-align:center;"><strong>M</strong></td>
                    <td style="color:#fff; background-color:#999; text-align:center;"><strong>T</strong></td>
                    <td style="color:#fff; background-color:#999; text-align:center;"><strong>W</strong></td>
                    <td style="color:#fff; background-color:#999; text-align:center;"><strong>T</strong></td>
                    <td style="color:#fff; background-color:#999; text-align:center;"><strong>F</strong></td>
                    <td style="color:#fff; background-color:#999; text-align:center;"><strong>S</strong></td>
                  </tr>

                  <?php
                     // The mktime() function returns the Unix timestamp for a date
                     // syntax: mktime(hour,minute,second,month,day,year,is_dst) 
                     $timestamp = mktime(0,0,0,$ctr,1,$cYear);
                     
                     // formats a local time/date
                     // syntax: date(format,timestamp) 
                     $maxday    = date("t",$timestamp);
                     
                     // returns an array that contains date and time information for a Unix timestamp
                     // syntax: getdate(timestamp)                  
                     $thismonth = getdate ($timestamp);               
                     
                     // The returning array contains ten elements with relevant information needed when formatting a date string:
                     // [seconds] - seconds
                     // [minutes] - minutes
                     // [hours] - hours
                     // [mday] - day of the month
                     // [wday] - day of the week
                     // [year] - year
                     // [yday] - day of the year
                     // [weekday] - name of the weekday
                     // [month] - name of the month                  
                     $startday  = $thismonth['wday'];

                     for ($i=0; $i<($maxday+$startday); $i++) {      
                     
                        // highlight the days that are "holidays"
                        if(in_array(($i - $startday + 1),$holidays[$ctr-1])) {
                           $str = "style='background-color: red; color: #fff;'";
                        }
                        else{
                           $str = "";
                        }
                       
                        // check if is the start of the week, if yes, create new row
                        if(($i % 7) == 0 ) {
                           echo "<tr>\n";
                        }
                        
                        if($i < $startday) {
                           echo "<td></td>\n";
                        }
                        else{
                           echo "<td align='center' valign='middle' height='20px' $str>". ($i - $startday + 1) . "</td>\n";
                        }
                        
                        if(($i % 7) == 6 ){ 
                           echo "</tr>\n";
                        }
                     }  
                   ?>
                 </table>
               </td>
            </tr>
         </table>
         </div>
   <?php 
         $ctr++; 
      } 
   ?>
   <br style="clear: both;" />
   </div>   
</div> 

Preview:


Attached Files:

Further reading: