Login   Register  
PHP Classes
elePHPant
Icontem

File: calendar.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Bob  >  bCalendar  >  calendar.php  >  Download  
File: calendar.php
Role: Class source
Content type: text/plain
Description: the main calendar class
Class: bCalendar
Compute the days and weeks of a month
Author: By
Last change:
Date: 2012-01-12 09:32
Size: 2,493 bytes
 

Contents

Class file image Download
<?php
class Calendar
{
    function 
num_weeks($month$year)
    {
        
$num_weeks=4;
    
        
$first_day $this->first_day($month$year);  
        
        
// if the first week doesn't start on monday 
        // we are sure that the month has at minimum 5 weeks
        
if($first_day!=1$num_weeks++;
        
        
$widows=$first_day-1;  
        
$fw_days=7-$widows;
        if(
$fw_days==7$fw_days=0;       
        
        
$numdays=date("t",mktime(200$month1$year));
        
        if( (
$numdays $fw_days) > 28 $num_weeks++;
        
        return 
$num_weeks;                  
    }
    
    
// this method returns array with the days 
    // in a given week. Always starts from Monday and return 7 numbers
    // if the day is there, returns the date, otherwise returns zero
    // very useful to build the empty cells of the calendar
    
function days($month$year$week$num_weeks=0)
    {
        
$days=array();
        
        if(
$num_weeks==0$num_weeks=$this->num_weeks($month$year);
        
        
// find which day of the week is 1st of the given month        
        
$first_day $this->first_day($month$year);
                
        
// find widow days (first week)
        
$widows=$first_day-1;
        
        
// first week days
        
$fw_days=7-$widows;
        
        
// if $week==1 don't do further calculations
        
if($week==1)
        {
            for(
$i=0;$i<$widows;$i++) $days[]=0;            
            for(
$i=1;$i<=$fw_days;$i++) $days[]=$i;            
            return 
$days;
        }
        
        
// any other week
        
if($week!=$num_weeks)
        {
            
$first=$fw_days+(($week-2)*7);
            for(
$i=$first+1;$i<=$first+7;$i++) $days[]=$i;            
            return 
$days;
        }
        
        
        
# only last week calculations below
        
        // number of days in the month
        
$numdays=date("t",mktime(200$month1$year));
                
        
// find orphan days (last week)  
        
$orphans=$numdays-$fw_days-(($num_weeks-2)*7);                     
        
$empty=7-$orphans;
        for(
$i=($numdays-$orphans)+1;$i<=$numdays;$i++) $days[]=$i;
        for(
$i=0;$i<$empty;$i++) $days[]=0;
        return 
$days;
    }
    
    function 
first_day($month$year)
    {
        
$first_daydate("w"mktime(200$month1$year));
        if(
$first_day==0$first_day=7# convert Sunday
        
        
return $first_day;
    }
}
?>