Thursday, 4 June 2015

PHP: Generate HTML Calendar of a month of year

<?php

/**
* calendar class :
* @package
* @access public
**/
class calendar{
    /**
     * Constructor
     * @access protected
     */
    function calendar(){
         
    }
     
    /**
     * calendar::html_month_calendar()
     *
     * @param integer $m        The month value, could be returned with $m = date("m");
     * @return
     **/
    function html_month_calendar($m = "",$y = ""){     
        if (trim($m) == "") {
            $m = date($m);
        }
        if (trim($y)=="") {
            $y = date("Y");
        }
        $curr_month = date("m");
        $d = date("d");
        if ($m==1) {
            $m_1 = 12;
            $y_1 = $y-1;
        } else {
            $m_1 = $m-1;
            $y_1 = $y;
        }
        $to = mktime(0,0,0,$m_1,0,$y_1);
        $days_in_month = strftime ("%d",$to);
        $from = mktime(0,0,0,$m,1,$y);
         
        $from_a = getdate($from);
        $to_a = getdate($to);
        $calendar = "\n<table bgcolor=white cellpadding=2 cellspacing=0>
<tr>
    <th>Dim</th>
    <th>Lun</th>
    <th>Mar</th>
    <th>Mer</th>
    <th>Jeu</th>
    <th>Ven</th>
    <th>Sam</th>
</tr>\n";

        $Days = 0;
         
        for($i = 1; $i <= 6; $i++){
            $calendar .= "<tr>\n";
            for($j = 0; $j <= 6 ; $j++){
                if ($d == ($Days+1) and $curr_month==$m) {
                    $b1 = "<b>";$b2 = "</b>";
                } else $b1 = "";$b2 = "";
                 
                if ($from_a["wday"]==$j and $Days==0) {
                    $Days++;
                    $calendar .= "<td>$b1 $Days $b2</td>";
                } elseif($Days==0) {
                    $calendar .= "<td></td>";
                } else {
                    $Days++;
                    if ($Days<=$days_in_month) {
                        $calendar .= "<td>$b1 $Days $b2</td>";
                    } else {
                        $calendar .= "<td></td>";
                    }
                }
            } // for
            $calendar .= "</tr>\n";
        } // for
        $calendar .= "</table><center>\n";
         
        return $calendar;
    }
     
    /**
     *
     * @access public
     * @return void
     **/
    function html_year_calendar($y = ""){
        if (trim($y)=="") {
            $y = date("Y");
        }
         
        $year_calendar = "<table bgcolor=white cellpadding=2 cellspacing=0 width=100% border=1>\n<tr>\n";
         
        for($i = 1; $i <= 12; $i++){
            $year_calendar .= "<td valign=top>Mois $i <br>".$this->html_month_calendar($i,$y )."</td>";
            if ($i == 3 or $i == 6 or $i == 9) {
                $year_calendar .= "</tr>\n<tr>\n";
            }
        } // for
         
        $year_calendar .= "</tr><table>";
         
        return $year_calendar;
    }
     
     
}

$cal = new calendar;
//echo $cal->html_month_calendar();
echo $cal->html_year_calendar();

?>

0 comments:

Post a Comment