標籤:style blog http color 使用 2014
抽空寫了個行事曆程式,只注重功能和實現的思路,所以代碼和功能都比較簡單,但是理解和擴充也比較容易。
show()函數用來顯示日曆,你可以修改show()函數,通過傳值的方式來實現顯示不同的年月。
<?php class Calendar{ public $weekarray = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六'); public $firstDay = '';//當月第一天 public $firstNum = '';//返回當月第一天對應的星期數字 public $firstDayNum = '';//當月第一天對應的中文星期 /** * 指定日是星期幾 * eg:$date='2014-07-21' */ public function getWeek($date){ $date = strtotime($date);//strtotime() 函數將任何英文文本的日期時間描述解析為 Unix 時間戳記 $num = date('w',$date);//數字型的星期幾,如: "0" (星期日) 至 "6" (星期六) return $num; } /** * 取得指定月有多少天 * eg:$month = '2014-07' */ public function getMonthNum($month){ $month = strtotime($month); return date('t',$month);//指定月份的天數 } /** * 顯示日曆 */ public function show(){ //取得當前日期 $year = date('Y'); $month = date('m'); $day = date('d'); echo '<table border="1" style="text-align:center;">'; echo '<tr><td colspan="7">'.$year.'-'.$month.'</td></tr>'; echo '<tr><th>星期日</th><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th>星期六</th></tr>'; //取得當前月有多少天 $yearMonth = '$year."-".$month'; $monthNum = $this->getMonthNum($yearMonth); //取得當前月第一天是星期幾 $this->firstDay = $year."-".$month."-01"; $this->firstNum = $this->getWeek($this->firstDay); $this->firstDayNum = $this->weekarray[$this->firstNum]; echo '<tr>'; for($i=1;$i<=$monthNum+($this->firstNum);$i++){ echo '<td>'; if($i >= $this->firstNum+1){ $a = $i-$this->firstNum; if($a == $day){ echo '<div style="background-color:blue;color:#fff;">'.$a.'</div>'; }else{ echo '<div>'.$a.'</div>'; } } echo '</td>'; if($i%7 == 0){//每輸出7列就換一行 echo '</tr>'; } } echo '</table>'; } } $calendar = new Calendar(); $calendar->show();
:
show()函數用來顯示日曆,你可以修改show()函數,通過傳值的方式來實現顯示不同的年月。
擴充:
本文的主要知識點在於PHP date()函數的使用
這裡有篇date()函數的詳解:http://wenku.baidu.com/link?url=OPDGzaCMWgjDE0ya8QlDbLIXX0c11ohUjsoLyRm-cYp7lz-O_7H4XBILv8JfomhbnSxXSW0FhqxYBK0_gn8Nr77XMWp-_st7v8AYecbNZjG