Location: PHPKode > scripts > EasyPhpEventCalendar Class > easyphpeventcalendar-class/easyphpeventcalendar.class.php
<?php

/**

EasyPhpEventCalendarPro Class version 1.0.1 

Copyright (c) 2009 JF Nutbroek <hide@address.com>
For more information visit http://wwww.mywebmymail.com

Permission to use, copy, modify, and/or distribute this software for any
purpose with fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

*/

class easyphpeventcalendar {
    
    /**
    * URL to gfx directory with calendar backgrounds
    *
    * @var string
    */        
    public $Gfxurl;
    /**
    * Days; array with 7 string values representing the days
    * [0]: Sunday
    * [1]: Monday
    * [2]: Tuesday
    * [3]: Wednesday
    * [4]: Thursday
    * [5]: Friday
    * [6]: Saturday
    *
    * @var array
    */        
    public $Days;
    /**
    * Months; array with 12 string values representing the months
    * [0]: January
    * [1]: February
    * [2]: March
    * [3]: April
    * [4]: May
    * [5]: June
    * [6]: July
    * [7]: August
    * [8]: September
    * [9]: October
    * [10]: November
    * [11]: December
    *
    * @var array
    */        
    public $Months;        
    /**
    * Events; array with arrays representing the events
    * [0]: array ('YYYYMMDD','Event 1 as text with html markup')
    * [1]: array ('YYYYMMDD','Event 2 as text with html markup')
    * [2]: etc.
    *
    * @var array
    */        
    public $Events;
        
    /**
    * Class constructor
    *
    */    
    public function __construct() {
         
        $this->Gfxurl  = 'gfx';           
        $this->Days    = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
        $this->Months  = array('January','February','March','April','May','June','July','August','September','October','November','December');
        $this->Events  = array();
            
    }

    /**
    * Creates Event Calendar CSS
    *
    */    
    public function CreateEventCalendarCSS() {
        
        $this->Gfxurl=rtrim($this->Gfxurl,'/');
        echo "<style type=\"text/css\">\n";                    
        echo ".eventtable {\n";
        echo "    text-align        : center;\n";
        echo "    font-family       : Verdana, Helvetica, Arial, sans-serif;\n";
        echo "    font-size         : 11px;\n";
        echo "    color             : #000000;\n";
        echo "    border-collapse   : separate;\n"; 
        echo "    border-spacing    : 2px;\n";
        echo "    width             : 410px;\n";                
        echo "}\n";                        
        echo ".eventweekmonth {\n";
        echo "    background-image  : url('$this->Gfxurl/weekmonth.gif');\n";
        echo "    background-repeat : no-repeat;\n";                
        echo "    height            : 25px;\n";
        echo "    width             : 40px;\n";
        echo "}\n";
        echo ".eventweekmonthactive {\n";  
        echo "    background-image  : url('$this->Gfxurl/weekmonth_event.gif');\n";
        echo "    background-repeat : no-repeat;\n";                
        echo "    height            : 25px;\n";
        echo "    width             : 40px;\n";                
        echo "    font-weight       : bold;\n";
        echo "}\n";                
        echo ".eventweekend {\n";
        echo "    background-image  : url('$this->Gfxurl/weekend.gif');\n";
        echo "    background-repeat : no-repeat;\n";
        echo "    height            : 19px;\n";
        echo "    width             : 40px;\n";                
        echo "}\n";
        echo ".eventweekendactive {\n";
        echo "    background-image  : url('$this->Gfxurl/weekend_event.gif');\n";
        echo "    background-repeat : no-repeat;\n";
        echo "    height            : 19px;\n";
        echo "    width             : 40px;\n";                    
        echo "    font-weight       : bold;\n";
        echo "}\n";
        echo ".eventday {\n";  
        echo "    background-image  : url('$this->Gfxurl/day.gif');\n";
        echo "    background-repeat : no-repeat;\n";
        echo "    height            : 19px;\n";
        echo "    width             : 40px;\n";    
        echo "}\n";                
        echo ".eventdayactive {\n";  
        echo "    background-image  : url('$this->Gfxurl/day_event.gif');\n";
        echo "    background-repeat : no-repeat;\n";
        echo "    height            : 19px;\n";
        echo "    width             : 40px;\n";                    
        echo "    font-weight       : bold;\n";
        echo "}\n";
        echo ".eventdaydisabled {\n";  
        echo "    background-image  : url('$this->Gfxurl/day.gif');\n";
        echo "    background-repeat : no-repeat;\n";
        echo "    height            : 19px;\n";
        echo "    width             : 40px;\n";                    
        echo "    color             : #969696;\n";
        echo "}\n";
        echo ".eventmonthdisabled {\n";  
        echo "    background-image  : url('$this->Gfxurl/weekend.gif');\n";
        echo "    background-repeat : no-repeat;\n";
        echo "    height            : 19px;\n";
        echo "    width             : 40px;\n";                    
        echo "    color             : #969696;\n";
        echo "}\n";
        echo ".eventdetails {\n";  
        echo "    background-image  : url('$this->Gfxurl/eventdetails.gif');\n";
        echo "    background-repeat : no-repeat;\n";
        echo "    text-align        : left;\n";
        echo "    vertical-align    : top;\n";
        echo "    height            : 160px;\n";
        echo "    width             : 304px;\n";                    
        echo "    padding           : 5px;\n";        
        echo "}\n";                  
        echo ".eventcalendartitle {\n";
        echo "    background-image  : url('$this->Gfxurl/title.gif');\n";
        echo "    background-repeat : no-repeat;\n";
        echo "    height            : 25px;\n";
        echo "    width             : 304px;\n";                    
        echo "    font-weight       : bold;\n";
        echo "}\n";
        echo "</style>\n";
        
    }

    /**
    * Creates Event Calendar
    *
    */    
    public function CreateEventCalendar() {

        // Get current date
        $date=time();$month=date('m',$date);$year=date('Y',$date);

        // Left column with months
        echo "<table class=\"eventtable\"><tr><td style=\"vertical-align : top;width : 44px;\">\n";
        echo "<table style=\"vertical-align : top;border-collapse : separate;border-spacing : 2px;width : 44px;\">\n";
        echo "<tr><td style=\"height : 25px;\">&nbsp;</td></tr>\n";
        if ($this->eventcheck(1)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
        echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div1')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[0],0,3) . "</td></tr>\n";
        if ($this->eventcheck(2)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
        echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div2')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[1],0,3) . "</td></tr>\n";
        if ($this->eventcheck(3)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
        echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div3')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[2],0,3) . "</td></tr>\n";
        if ($this->eventcheck(4)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
        echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div4')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[3],0,3) . "</td></tr>\n";
        if ($this->eventcheck(5)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
        echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div5')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[4],0,3) . "</td></tr>\n";
        if ($this->eventcheck(6)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
        echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div6')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[5],0,3) . "</td></tr>\n";
        echo "</table>\n";
        echo "</td><td style=\"vertical-align : top;width : 308px;\">\n";

        // Add Events
        foreach ($this->Events as $key => $event) {
            $eventmonth=intval(substr($event[0],4,2));$eventyear=intval(substr($event[0],0,4));$eventday=intval(substr($event[0],6,2));
            if (checkdate($eventmonth,$eventday,$eventyear)) {
                $day_no=date('N',mktime(0,0,0,$eventmonth,$eventday,$eventyear));$month_no=date('n',mktime(0,0,0,$eventmonth,$eventday,$eventyear))-1;
                if ($day_no==7) {$day_no=0;}
                $date=$this->Days[$day_no] . ' ' . $eventday . ' ' . $this->Months[$month_no] . ' ' . $eventyear;
                echo "<div id=\"div" . ($key + 13) ."\" style=\"display: none\">\n";
                echo "<table style=\"width : 308px;vertical-align : top;border-collapse : separate;border-spacing : 2px;\"><tr><td class=\"eventcalendartitle\">$date</td></tr><tr><td class=\"eventdetails\" onclick=\"expandCollapse('div$eventmonth')\" onmouseover=\"this.style.cursor='pointer';\">" . $event[1] . "</td></tr></table>\n";
                echo "</div>\n";
            }
        }
       
        // Add Months
        for ($m=1;$m<13;$m++) {
            if ($m==$month) {
                echo "<div id=\"div$m\">\n";
                $this->eventcalendar(mktime(0,0,0,$m,1,$year));
                echo "</div>\n";
            } else {
                if ($m<$month) {$i=1;} else {$i=0;}
                echo "<div id=\"div$m\" style=\"display: none\">\n";
                $this->eventcalendar(mktime(0,0,0,$m,1,$year+$i));
                echo "</div>\n";        
            }
        }

        // Right column with months
        echo "</td><td style=\"vertical-align : top;width : 44px;\">\n";
        echo "<table style=\"width : 44px;vertical-align : top;border-collapse : separate;border-spacing : 2px;\">\n";
        echo "<tr><td style=\"height : 25px;\">&nbsp;</td></tr>\n";
        if ($this->eventcheck(7)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
        echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div7')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[6],0,3) . "</td></tr>\n";
        if ($this->eventcheck(8)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
        echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div8')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[7],0,3) . "</td></tr>\n";
        if ($this->eventcheck(9)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
        echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div9')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[8],0,3) . "</td></tr>\n";
        if ($this->eventcheck(10)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
        echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div10')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[9],0,3) . "</td></tr>\n";
        if ($this->eventcheck(11)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
        echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div11')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[10],0,3) . "</td></tr>\n";
        if ($this->eventcheck(12)) {$class='eventweekmonthactive';} else {$class='eventweekmonth';}
        echo "<tr><td class=\"$class\" onclick=\"expandCollapse('div12')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">". substr($this->Months[11],0,3) . "</td></tr>\n";
        echo "</table>\n";
        echo "</td></tr></table>\n";
                
    }
        
    /**
    * Check if month has an event
    *
    */
      private function eventcheck($month) {
    
        foreach ($this->Events as $key => $event) {
            if (intval(substr($event[0],4,2))==$month) 
                return true;
        }
        return false;
                
    }
            
    /**
    * Print the month
    *
    */
    private function eventcalendar($date) {
    
        $day=date('d',$date);$month=date('m',$date);$year=date('Y',$date);$first_day=mktime(0,0,0,$month,1,$year);$title=$this->Months[date('n',mktime(0,0,0,$month,$day,$year))-1]; 
        $days_in_month=cal_days_in_month(0,$month,$year);$day_of_week=date('N',$first_day); if ($day_of_week==7) {$blank=0;} else {$blank=$day_of_week;}
        echo "<table style=\"vertical-align : top;border-collapse : separate;border-spacing : 2px;width : 308px;\">";echo "<tr><td colspan=\"7\" class=\"eventcalendartitle\">$title $year</td></tr>";
        echo "<tr><td class=\"eventweekmonth\">". substr($this->Days[0],0,3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[1],0,3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[2],0,3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[3],0,3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[4],0,3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[5],0,3) . "</td><td class=\"eventweekmonth\">". substr($this->Days[6],0,3) . "</td></tr>";
        if (($month-1)==0) {$prevmonth=1;$prevyear=($year-1);} else {$prevmonth=($month-1);$prevyear=$year;}
        $day_prevmonth=cal_days_in_month(0,$prevmonth,$prevyear)-($blank-1);echo "<tr>";
        $day_num=1;$day_count=1;$datenow=time();$monthnow=date('n',$datenow);$yearnow=date('Y',$datenow);$daynow=date('j',$datenow);
        if ($blank>0) {echo "<td class=\"eventmonthdisabled\">$day_prevmonth</td>";$blank=$blank-1;$day_count++;$day_prevmonth++;} 
        while ($blank>0) {echo "<td class=\"eventdaydisabled\">$day_prevmonth</td>";$blank=$blank-1;$day_count++;$day_prevmonth++;} 
        while ($day_num<=$days_in_month) {    
            $class="class=\"eventday\"";$id=0;
            if ($day_count==1 || $day_count==7) {$class="class=\"eventweekend\"";}
            foreach ($this->Events as $key => $event) {
                $eventyear=intval(substr($event[0],0,4));$eventmonth=intval(substr($event[0],4,2));$eventday=intval(substr($event[0],6,2));
                if ($eventyear==$year && $eventmonth==$month && $eventday==$day_num) {
                $class="class=\"eventdayactive\"";
                if ($day_count==1 || $day_count==7) {$class="class=\"eventweekendactive\"";}   
                $id=($key +13);break;
                }       
            }
            if ($yearnow==$year && $monthnow==$month && $daynow==$day_num) {$textstyle='style="color : #FF0000;font-weight : bold;"';} else {$textstyle='';}
            if ($id>0) {
                echo "<td $class $textstyle onclick=\"expandCollapse('div$id')\" onmouseover=\"this.style.cursor='pointer';this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\"> $day_num </td>"; 
            } else {
                echo "<td $class $textstyle> $day_num </td>"; 
            }
            $day_num++;$day_count++;
            if ($day_count>7) {echo "</tr><tr>";$day_count=1;}
        }
        $day_nextmonth=1;
        if ($day_count==1) {
            echo "<td colspan=\"7\">&nbsp;</td>";
        } else {
            while ($day_count>1 && $day_count<=7 ) {
                if ($day_count==7)
                    echo "<td class=\"eventmonthdisabled\">$day_nextmonth</td>";
                else
                    echo "<td class=\"eventdaydisabled\">$day_nextmonth</td>";
                $day_count++;$day_nextmonth++;
            }
        }
        echo "</tr></table>\n";       
  }
        
}

?>
Return current item: EasyPhpEventCalendar Class