簡單的php教程日曆類控制項代碼實例
/*
本文章主要是一個php初學者寫的原創的php日曆控制項 ,可以顯示當前日期與今天是星期幾,是否為閏年, 可自動選擇上一年或下一年,月份日期也是一樣的。
*/
date_default_timezone_set("Etc/GMT-8");
class Calendar{
var $T = array();
var $datesOFmonth = array('1'=>'31','2'=>'28','3'=>'31','4'=>'30','5'=>'31','6'=>'30','7'=>' 31','8'=>'31','9'=>'30','10'=>'31','11'=>'30','12'=>'31');
var $Y,$M,$D;
function set($time){
$this->T = getdate($time);
$this->Y = $this->T['year'];
$this->M = $this->T['mon'];
$this->D = date('d',$time);
}
function isRun(){
return ($this->Y%400==0 || ($this->Y%4==0 && $this->Y%100==0)) ? 1 : 0;
}
function first(){
$time = mktime(0,0,0,$this->M,1,$this->Y);
$time = getdate($time);
return $time['wday'];
}
function html(){
$isRun = $this->isRun();
$this->datesOFmonth[2] = $isRun==1 ? 29: 28;
$html .= "< table style='border:solid 1px black;' >n";
$html .= "<tr><th><a href=''>上一月</a></th><th colspan='5'>{$this->Y}年 {$ this->M}月</th><th><a href=''>下一月</a></th><tr>n";
$html .= "<tr><td>星期天</td><td>星期一</td><td>星期二</td><td>星期三 </td><td>星期四</td><td>星期五</td><td>星期六</td></tr>n";
$html .= "<tr>n";
$first = $this->first();
for($i=0; $i<$first; $i++){
$html .= "<td></td>";
}
$count = $this->datesOFmonth[$this->M]+$first;
for ($i=1; $i<= $this->datesOFmonth[$this->M]; $i++){
$style = $i==$this->D ? ' style="color:red;font-weight:bold;"' : '' ;
$html .= "<td align='center'{$style}>$i</td>";
if (($i==7%$first || ($i+$first)%7==0) && $i<$count){
$html .= "</tr>n<tr>";
}
}
$count = 7-$count%7;
if ($count<7){
for ($i=0; $i<$count; $i++){
$html .= "<td></td& gt;";
}
}
$html .= "</tr>n";
$html .= "</table>n";
return $html;
}
}
$calendar = new Calendar();
$calendar->set(time());
echo $calendar->html();