PHP Classes

File: class.calendar.php

Recommend this page to a friend!
  Classes of Suranga Niroshan Fernando   Calendar Display   class.calendar.php   Download  
File: class.calendar.php
Role: Class source
Content type: text/plain
Description: class file
Class: Calendar Display
Display calendar months with special day marks
Author: By
Last change: uploading the file
Date: 13 years ago
Size: 3,539 bytes
 

Contents

Class file image Download
<?php
/*
@Author - W W Suranga Niroshan Fernando.
@Description - Prints the month calendar foa given month and year
                Prints marked dates like reminders holidays and
                and Week Ends

*/
class calendar{
   
    private
$markedDates;
    public
$month;
    public
$year;
    public
$monthPrefix =array(
       
'0'=>'Jan',
       
'1'=>'Feb',
       
'2'=>'Mar',
       
'3'=>'Apr',
       
'4'=>'May',
       
'5'=>'Jun',
       
'6'=>'Jul',
       
'7'=>'Aug',
       
'8'=>'Sep',
       
'9'=>'Oct',
       
'10'=>'Nov',
       
'11'=>'Dec'
   
);
   
    function
__construct(){
    }
   
/*
        Set Month and year for calendar write
    */
   
function setDate($month, $year){
       
$this->month=$month;
       
$this->year=$year;
    }
   
/*
        Set marked dates array to bind when writing the calendar
       
    */
   
function setMarkedDates($markedDates){
       
$this->markedDates=$markedDates;
    }
   
/*
        Writting the calander to the out put
    */
   
function writeCalander(){
       
       
/*
        Setting next and prev months in case month is last or first this sets the prev year accordingly
        */
       
if($this->month==null or $this->year==null or $this->month >12 or $this->month <1 or $this->year < 1 ){
            Throw new
Exception("Calander Exception :::::::: Given monthand year is not valid");
            die();
        }
       
$prevMonthNum = ($this->month==1)? 12: $this->month-1;
       
$prevYearNum = ($this->month==1)? $this->year-1: $this->year;
       
       
$nextMonthNum = ($this->month==12)? 1: $this->month+1;
       
$nextYearNum = ($this->month==12)? $this->year+1: $this->year;
       
       
// get the number of dates in the month
       
$numOfDates = cal_days_in_month(CAL_GREGORIAN, $this->month, $this->year) ;
       
       
$numOfDatesPrevMonth = cal_days_in_month(CAL_GREGORIAN, $prevMonthNum, $prevYearNum);
       
$timeStamps = mktime(0, 0, 0, $this->month, 1, $this->year);
       
$timeStampe = mktime(0, 0, 0, $this->month, $numOfDates, $this->year);
       
// Starting day of the month this returns int between 0 - 6
       
$s= date("w", $timeStamps) ;
       
// End day of the month this returns int between 0 - 6
       
$e=date("w", $timeStampe) ;
       
       
// intializing array to contain all the dates which needs to display include prev and next months dates
       
$datesArray = array();
       
       
$begin = ($s==0)?1:1-$s;
       
$end = ($e==0)? ($numOfDates+1): $numOfDates + (7-$e);
       
       
$a=0;
        for(
$i=$begin;$i<$end;$i++){
           
$datesArray[$a] =date("Y-m-d", mktime(0, 0, 0, $this->month, $i, $this->year));
           
$a++;
        }
        echo <<<EOD
<table border="1">
        <tr>
            <th>Sunday</th>
            <th>Monday</th>
            <th>Tuesday</th>
            <th>Wendsday</th>
            <th>Thursday</th>
            <th>Friday</th>
            <th>Staurday</th>
        </tr>
EOD;
       
$j=0;
        for(
$i=0;$i<(count($datesArray)/7); $i++){
            echo <<<EOD
<tr>
EOD;
            for(
$x=0;$x<7;$x++){
           
$classes;
           
$rel;
           
/* Check for marked dates
                If This date is a marked dtae print information togeather with the date
            */
           
if (array_key_exists($datesArray[$j],$this->markedDates)){
               
$classes = $this->markedDates[$datesArray[$j]]['type'] ." ";
               
$rel = $this->markedDates[$datesArray[$j]]['title'];
            }else {
               
            }
           
$date = explode("-",$datesArray[$j]);
   
            if(
$date[1]==$this->month){
               
$classes .="current_month";
            }else{
               
$classes .="other-months";
            }
            echo <<<EOD
<td class="$classes" alt="" rel="$rel">{$date[2]}</td>
EOD;
               
$classes=null;
               
$j++;
            }
        echo <<<EOD
</tr>
EOD;
        }
       
    echo <<<EOD
</table>
EOD;
    }
    function
setCurrentDetails(){
        echo
"<h2>".$this->monthPrefix[$this->month-1]." - " . $this->year ."</h2>";
    }
}
?>