<?php

class calendar{

    var 
$thedate;
    var 
$startday;
    var 
$tabletags;
    var 
$tdinmonth;
    var 
$tdoutmonth;
    var 
$tdtoday;
    var 
$tdtodayevent;
    var 
$tdevent;
    var 
$tdoutevent;
    var 
$dayformat;
    var 
$thtags;
    var 
$themonth;
    var 
$theday;
    var 
$theyear;
    var 
$firstofmonthts;

    function 
setdate($thedate){
        
$bits split("-"$thedate);
        
$this->thedate $thedate;
        
$this->themonth = (int)$bits[1];
        
$this->theyear $bits[0];
        
$this->theday = (int)$bits[2];
        
$this->firstofmonthts mktime(0,0,0,$this->themonth1$this->theyear);
    }

    function 
dayofweek(){
        return 
date("w"strtotime($this->thedate));
    }

    function 
notaweekend($frimon = -1){
        
$day $this->dayofweek();
        if( 
$day == ){    // sunday
            
$this->setdate$this->dateadd"%Y-%m-%d"$frimon == -? -1));
        }elseif( 
$day == ){    // saturday
            
$this->setdate($this->dateadd("%Y-%m-%d"$frimon == -? -2));
        }
    }

    function 
addweekdays($format "%Y-%m-%d"$days=0){
        
$inc $days ? -1;
        
$days abs($days);
        
$firstday $this->dayofweek();
        
$numdays 0;
        
$day 0;
        while( 
$day $days ){
            if( 
$firstday != && $firstday != $day++;
            
$numdays++;
            
$firstday += $inc;
            if( 
$firstday $firstday 6;
            if( 
$firstday $firstday 0;
        }
        return 
strftime($formatmktime(0,0,0,$this->themonth$this->theday+($numdays $inc), $this->theyear));
    }

    function 
dateadd($format "%Y-%m-%d"$days 0$months 0$years 0$keepday true){
        if( 
$days == && $keepday == true ){
            
$daysinnewmonth date("t"mktime(0,0,0,$this->themonth+$months$this->theyear+$years));
            if( 
$daysinnewmonth $this->theday ){
                return 
strftime($formatmktime(0,0,0,$this->themonth+$months$daysinnewmonth$this->theyear+$years));
            }else{
                return 
strftime($formatmktime(0,0,0,$this->themonth+$months$this->theday+$days$this->theyear+$years));
            }
        }else{
            return 
strftime($formatmktime(0,0,0,$this->themonth+$months$this->theday+$days$this->theyear+$years));
        }
    }

    function 
getdate($format "%Y-%m-%d"){return strftime($formatmktime(0,0,0,$this->themonth$this->theday$this->theyear));}
    function 
nextmonth($format "%Y-%m-%d"){return $this->dateadd($format010);}
    function 
prevmonth($format "%Y-%m-%d"){return $this->dateadd($format0, -10);}
    function 
nextday($format "%Y-%m-%d"){return $this->dateadd($format100false);}
    function 
prevday($format "%Y-%m-%d"){return $this->dateadd($format, -10,0false);}
    function 
nextyear($format "%Y-%m-%d"){return $this->dateadd($format001);}
    function 
prevyear($format "%Y-%m-%d"){return $this->dateadd($format00, -1);}
    function 
nextweek($format "%Y-%m-%d"){return $this->dateadd($format700false);}
    function 
prevweek($format "%Y-%m-%d"){return $this->dateadd($format, -70,0false);}

    function 
daysinmonth(){
        return (int)
date("t"mktime(0,0,0,$this->themonth1$this->theyear));
    }

    function 
calendar($thedate ""$startday 0){
        global 
$HTTP_SERVER_VARS;
        if( 
$thedate == "" $thedate date("Y-m-d");
        
$this->setdate($thedate);
        
$this->startday $startday;
        
$this->tabletags "<table>";
        
$this->tdinmonth "<td width='32' height='32'><a href='".$HTTP_SERVER_VARS["PATH_INFO"]."?thedate=%date%'>%day%</a></td>\n";
        
$this->tdoutmonth "<td>%day%</td>\n";
        
$this->tdinmonthevent "<td><a href='".$HTTP_SERVER_VARS["PATH_INFO"]."?thedate=%date%'>%day%</a><br>%event%</td>\n";
        
$this->tdoutmonthevent "<td>%day%<br>%event%</td>\n";
        
$this->dayformat "full";
        
$this->thtags "<th width='32'>%day%</th>";
        
$this->tdtoday "<td bgcolor='#bbbbbb'><a href='".$HTTP_SERVER_VARS["PATH_INFO"]."?thedate=%date%'>%day%</a></td>\n";
        
$this->tdtodayevent "<td bgcolor='#bbbbbb'><a href='".$HTTP_SERVER_VARS["PATH_INFO"]."?thedate=%date%'>%day%</a><br>%event%</td>\n";
    }

    function 
getcalendardays(){
        
$daysinmonth $this->daysinmonth();
        
$firstdayofmonth date("w"$this->firstofmonthts);
        
$lastdayofmonth date("w"mktime(0,0,0,$this->themonth$daysinmonth$this->theyear));
        
$daysinlastmonth date("t"mktime(0,0,0,$this->themonth-11$this->theyear));
        
$daysback $firstdayofmonth $this->startday;
        if( 
$daysback ){
            
$daysback += 7;
        }
        
$days = array();
        for( 
$i $daysback-1$i >= 0$i--) $days[] = $daysinlastmonth $i;
        for( 
$i 1$i <= $daysinmonth$i++){
            
$days[] = $i;
        }
        if( 
count($days) % != ){
            
$daysremaining - (count($days) % 7);
            for( 
$i 1$i <= $daysremaining$i++) $days[] = $i;
        }
        return 
$days;
    }

    function 
getdaynames(&$days$themonth$theyear){
        
$daynames = array();
        switch (
$this->dayformat){
            case 
"full":
                
$dformat "%A";
                break;
            default:
                
$dformat "%a";
                break;
        }
        for( 
$i 0$i 7$i++){
            if( 
$days[$i] > ){
                
$daynames[$i] = strftime$dformatmktime(0,0,0,$themonth-1$days[$i], $theyear));
            }else{
                
$daynames[$i] = strftime$dformatmktime(0,0,0,$themonth$days[$i], $theyear));
            }
            if( 
$this->dayformat == "short" $daynames[$i] = substr($daynames[$i], 01);
        }
        return 
$daynames;
    }

    function 
displaycalendar($events = array()){
        
$thedays $this->getcalendardays();
        
$dayofweek 0;
        
$inmonth 0;
        
$daynames $this->getdaynames($thedays$this->themonth$this->theyear);
        echo 
"<table border=1 cellpadding=0 cellspacing=0 align=center>\n";
        echo 
"<tr>\n";
        for( 
$i 0$i 7$i++)    echo str_replace("%day%"$daynames[$i], $this->thtags);
        echo 
"</tr>\n";
        
$search = array("%day%""%date%""%event%");
        for( 
$i 0$i count($thedays); $i++){
            
$event "";
            if( 
$dayofweek == ){
                echo 
"<tr>\n";
            }elseif( 
$dayofweek == ){
                echo 
"</tr>\n";
                
$dayofweek 0;
            }
            if( 
$thedays[$i] == ){
                
$inmonth $inmonth;
            }
            if( 
$inmonth ){
                
$thedate date("Y-m-d"mktime0,0,0,$this->themonth$thedays[$i], $this->theyear));
                if( 
$thedays[$i] == $this->theday ){
                    if( isset(
$events["$thedate"]) ){
                        
$out $this->tdtodayevent;
                        
$event $events["$thedate"];
                    }else{
                        
$out $this->tdtoday;
                    }
                }else{
                    if( isset(
$events["$thedate"]) ){
                        
$out $this->tdinmonthevent;
                        
$event $events["$thedate"];
                    }else{
                        
$out $this->tdinmonth;
                    }
                }
            }else{
                if( 
$thedays[$i] < ){
                    
$thedate date("Y-m-d"mktime(0,0,0,$this->themonth+1$thedays[$i], $this->theyear));
                }else{
                    
$thedate date("Y-m-d"mktime(0,0,0,$this->themonth-1$thedays[$i], $this->theyear));
                }
                if( isset(
$events["$thedate"]) ){
                    
$out $this->tdoutmonthevent;
                    
$event $events["$thedate"];
                }else{
                    
$out $this->tdoutmonth;
                }
            }
            echo 
str_replace($search,array($thedays[$i], $thedate$event), $out);
            
$dayofweek++;
        }
        if( 
$dayofweek == ) echo "</tr>\n";
        echo 
"</table>\n";
    }

}

?>