calendar.class.phpphp
<?php date_default_timezone_set('PRC'); class Calendar{ private $year; //當前的年 private $month; //當前的月 private $day; //當前的天 private $start_weekday; //當月開始的第一天是周幾 private $days; //當前月一共多少天 function __construct(){ //得到當前的年 $this->year=isset($_GET['year'])?$_GET['year']:date("Y"); //得到當前的月 $this->month=isset($_GET['month'])?$_GET['month']:date("m"); //得到當前的日 $this->day=date("d"); //得到這個月的天數 $this->start_weekday=date("w",mktime(0,0,0,$this->month,1,$this->year)); //得到這個月開始的第一天是星期幾 $this->days=date("t",mktime(0,0,0,$this->month,1,$this->year)); } function out(){ echo '<table align="center">'; //調用改變年和月的函數 $this->changeDate("test.php"); //調用星期列表 $this->weeksList(); //調用日期列表 $this->daysList(); echo '</table>'; } private function weeksList(){ //建立星期數組 $week=array('日','一','二','三','四','五','六'); echo '<tr>'; //輸出星期數組 for($i=0;$i<count($week);$i++){ echo '<th class="fontb">'.$week[$i].'</th>'; } echo '</tr>'; } private function daysList(){ echo '<tr>'; //輸出空格(當前一個月前面空出來的) for($j=0;$j<$this->start_weekday;$j++){ echo '<td> </td>'; } //輸出這個月的日期 for($k=1;$k<$this->days;$k++){ $j++; if($k==$this->day) echo '<td class="fontb">'.$k.'</td>'; else echo '<td>'.$k.'</td>'; if($j%7==0) echo '</tr><tr>'; } //後面的空格 while($j%7!=0){ echo '<td> </td>'; $j++; } echo '</tr>'; } private function prevYear($year,$month){ $year=$year-1; if($year < 1970) $year=1970; return "year={$year}&month={$month}"; } private function prevMonth($year,$month){ if($month==1){ $year=$year-1; if($year < 1970) $year=1970; $month=1; }else{ $month--; } return "year={$year}&month={$month}"; } private function nextYear($year,$month){ $year=$year+1; if($year > 2038) $year=2038; return "year={$year}&month={$month}"; } private function nextMonth($year,$month){ if($month==12){ $year=$year+1; if($year > 2038) $year=2038; $month=12; }else{ $month++; } return "year={$year}&month={$month}"; } private function changeDate($url=""){ echo '<tr>'; echo '<td colspan="7">'; echo '<form>'; echo '<select name="year" onchange="window.location=\''.$url.'?year=\'+this.options[selectedIndex].value+\'&month='.$this->month.'\'">'; for($sy=1970; $sy <= 2038; $sy++){ $selected = ($sy==$this->year) ? "selected" : ""; echo '<option '.$selected.' value="'.$sy.'">'.$sy.'</option>'; } echo '</select>'; echo '<select name="month" onchange="window.location=\''.$url.'?year='.$this->year.'&month=\'+this.options[selectedIndex].value">'; for($sm=1; $sm<=12; $sm++){ $selected1 = ($sm==$this->month) ? "selected" : ""; echo '<option '.$selected1.' value="'.$sm.'">'.$sm.'</option>'; } echo '</select>'; echo '</form>'; echo '</td>'; echo '</tr>'; } } ?>
calendar.php數組
<style> table{ border:1px solid #050; } .fontb{ color:white; background: blue; } th,td{ text-align: center; width:30px; height: 30px; } form{ margin: 0px; padding: 0px; } select{ text-align: center; } </style> <?php //包含類文件 include "./calendar.class.php"; //建立類對象 $calendar=new Calendar; $calendar->out(); ?>