Zend - The PHP Company




Calendars

Add Code


Calendar class  

Type: class library
Added by: jthome_zend
Entered: 30/10/2003
Last modified: 02/11/2002
Rating: - (fewer than 3 votes)
Views: 5048
Full customizable class to print a calendar month. Template driven, very easy to customize for international use, and designed to take date arguments in a variety of ways.


<?
/* createCalendar class, version 1.0
* Written by Jim Thome 10/2003
* based on a code snippet by Martin Wallgren
*
* This code is free to use.
*
*
*
*/

class createCalendar{
        
//you can re-order the $days_of_week variable to suit your needs (for different countries)
        
var $days_of_week = array('Sun''Mon''Tue''Wed''Thu''Fri''Sat');

        
//you can reset these to false before calling the member functions
        
var $printheader true;
        var 
$bold_today true;

        
//these are examples of how to change the layout, using CSS here works too...
        
var $week_row_attrs=' align="center" bgcolor="#FFFFFF"';
        var 
$week_cell_attrs" onmouseover="javascriptthis.style.backgroundColor '#FFFFCC';
                            
" onmouseout="javascriptthis.style.backgroundColor '#FFFFFF';"";
        var 
$week_header_row_attrs='bgcolor="#FFFFFF"';
        var 
$table_attrs ' width="80%"  height="80%" align="center" border="0"
                                cellspacing="1" cellpadding="6" bgcolor="#999999"'
;
        var 
$header_row_attrs ='bgcolor="#FFFFFF" align="center"';

        function 
createCalendar($year$month$day 1) {
                
$this->timestamp = @mktime(000$month$day$year);
                if (!
$this->timestamp || $year<1970)
                    die(
"invalid date received: year=$year, month=$month, day=$day");

                
$this->days_in_month intval(date("t"$this->timestamp));

                
//month and year are re-calculated to allow for arguments like $day=32, $month=12+1, etc.
                
$this->month =  date('n'$this->timestamp);
                
$this->year =  date('Y'$this->timestamp);
                
$this->month_starts_on date("D"mktime(000$this->month1$this->year));

        } 
// end makeCalendar

        
function make_month_array(){
            
$i=1;
            
$this->week_num 0;
            while(
$i<$this->days_in_month+1){
                for(
$daynum=0;$daynum<count($this->days_of_week);$daynum++){
                    if (
$i==1){
                        if (
$this->month_starts_on==$this->days_of_week[$daynum]){
                            
$this->month_array[$this->week_num][$this->days_of_week[$daynum]]=$i;
                            
$i++;
                        } else {
                            
$this->month_array[$this->week_num][$this->days_of_week[$daynum]]='&nbsp;';
                        }
                    } else {
                        if (
$i<=$this->days_in_month){
                            
$this->month_array[$this->week_num][$this->days_of_week[$daynum]]=$i;
                        } else {
                            
$this->month_array[$this->week_num][$this->days_of_week[$daynum]]='&nbsp;';
                        }
                        
$i++;
                    }

                    if (
$this->bold_today===true
                            
&& $i-1==date('j')
                            && 
$this->month==date('n')
                            && 
$this->year==date('Y')
                            ){
                       
$this->month_array[$this->week_num][$this->days_of_week[$daynum]] =
                       
'<strong>'.$this->month_array[$this->week_num][$this->days_of_week[$daynum]] . '</strong>';
                    }

                }
                
$this->week_num++;
            }
            return 
$this->month_array;
        }

        function 
make_day_headers(){
            
$width=intval(1/count($this->days_of_week)*100) . "%";
            foreach(
$this->days_of_week as $day_names){
                
$htm .= "n<th width="$width">$day_names</th>";
            }
            return 
"n<tr {$this->week_header_row_attrs}>$htm</tr>";
        }

        function 
make_day_rows($array){
            foreach(
$array as $week_num=>$week_array){
                foreach (
$week_array as $value){
                    
$htm .= "n<td {$this->week_cell_attrs}>$value</td>";
                }
                
$rows .=   "n<tr {$this->week_row_attrs}>$htm</tr>";
                unset(
$htm);
            }
            return 
$rows;
        }

        function 
build_calendar(){
            
$month_array $this->make_month_array();
            
$headers $this->make_day_headers();
            
$make_day_rows $this->make_day_rows($month_array);
            return 
$this->month_template($headers$make_day_rows);
        }

        function 
month_template($day_headings$rows){
            if (
$this->printheader){
                
$month =  date('F'$this->timestamp);
                
$colspan=count($this->days_of_week);
                
$header = <<<EOT
                    <tr {$this->header_row_attrs}>
                    <th colspan="
$colspan">$month</th>
                    </tr>
EOT;
            }
        return <<<EOT
        <table {$this->table_attrs}>
        
$header
        
$day_headings
        
$rows
        </table>
EOT;

            }
//end class


$CalObj = new  createCalendar(19701232truetrue);
//you can change any of the
//html variables here before calling build_calendar()
$CalObj->week_header_row_attrs='bgcolor="#EEEEEE"';

//display the calendar
echo $CalObj->build_calendar();

?>


Usage Example




Rate This Script





Search



This Category All Categories