Zend - The PHP Company




Calendars

Add Code


XCalendar  

Type: class
Added by: JRandTheKing
Entered: 28/07/2000
Last modified: 08/12/1999
Rating: **** (3 votes)
Views: 7843
This is an extensible calendar class. It doesn't print any HTML for you, but it gives you easy straight-forward access to all the days of a given month.


<?php

    
/*
     * day objects are not real objects, just arrays.
     * $calendar->nextDay();
     * $day = $calendar->currentDay;
     * $day[0] = date, $day[1] = day of week
     *
     * function DaysInMonth taken from php mailing list
     * posted by John Coggeshall
     */

class XCalendar {

    var 
$MAX_DAYS;
    var 
$dayPointer =  0;
    var 
$weekPointer 0;
    var 
$currentDay;
    var 
$dayArray = array();
    var 
$skipEnds =false;

    var 
$ERR_MSG;

    
//**********************
    // constructors & inits
    //**********************

    
function XCalendar($month,$year) {

        
$tmp date("d | w",mktime(0,0,0,$month,1,$year));
        
$firstDay explode(" | ",$tmp);


        
$this->MAX_DAYS $this->DaysInMonth($month,$year);

        
// fill in week with blanks until first of month
        
for($x=0$x $firstDay[1]; $x++) {
        
$this->dayArray[]  = array("",-1);
        }

        
// fill in rest of days
        
$y $firstDay[1];
        for(
$x=1$x <= $this->MAX_DAYS$x++) {
        
$this->dayArray[] = array($x,$y);
        if( 
$y == ) {$y 0;} else { $y++;}
        }

        for(
$x=$y;$x <= 6$x++) {
        
$this->dayArray[] = array("",-1);
        
        }

        
    }

       


    
//*********
    // methods
    //*********

    
function nextDay() {

       if( 
$this->dayPointer count($this->dayArray) ) {
        
$this->ERR_MSG "no more days";
        
$this->currentDay = array("",-1);
        return 
false;
       }

       
$curDay $this->dayArray[$this->dayPointer];

       if( 
$this->skipEnds ) {
        if( 
$curDay[1] == ) { $this->dayPointer +=;}
        if( 
$curDay[1] == ) { $this->dayPointer ++; }
        
$curDay $this->dayArray[$this->dayPointer];
       }
       
$this->dayPointer++;
       
$this->currentDay $curDay;
       return 
true;

    }

    function 
hasMoreDays() {
        return (
$this->dayPointer count($this->dayArray));
    }

    function  
DaysInMonth($month,$year)
      {
        return
        
31-((($month-(($month<8)?1:0))%2)+(($month==2)?((!($year%((!($year%100))?400
        
:4)))?1:2):0));
      }

}
?>


Usage Example


    function constructTable($m,$y) {
        include("XCalendar.php");

        $calendar = new XCalendar($m,$y);
        $HTML = "<table border=2>n";

        while($calendar->hasMoreDays()) {
        $HTML .=" <tr>";
            for($z=0;$z < 7; $z++) {

        $calendar->nextDay();
        $day = $calendar->currentDay;
        if( $day[1] == 6 || $day[1] == 0 ) {
        $HTML .= "<td bgcolor="#C0C0C0">".$day[0]."</td>";
        } else {
        $HTML .= "<td>".$day[0]."</td>";
        }
        }

            $HTML .=" </tr>nn";
        }
        $HTML .="</table>n";
        return $HTML;
    }
$july = constructTable(7,2000);
print $july;


Rate This Script





Search



This Category All Categories