PHP 制作的日历一份
2015-01-13 16:46
204 查看
<style> *{margin:0;padding:0;list-style-type:none;font-family:"微软雅黑";} td,th{text-align:center;height:30px;width:40px;line-height:30px;} th{background:#CCCCCC;} .week{background:#FFCC33;} table{border:1px solid #06F;background:#0CF;} </style> <?php header("content-type:text/html;charset=utf-8"); class Calendar { private $year; private $month; private $day_week; private $start = 1970; private $stop = 2036; private $weeks = array("日", "一", "二", "三", "四", "五", "六"); private $months = array("01" => "一月", "02" => "二月", "03" => "三月", "04" => "四月", "05" => "五月", "06" => "六月", "07" => "七月", "08" => "八月", "09" => "九月", "10" => "十月", "11" => "十一月", "12" => "十二月"); function __construct(){ $this->year=isset($_POST['year']) ? $_POST['year'] : date("Y"); $this->month=isset($_POST['month']) ? $_POST['month'] : date("m"); $this->day_week=date("w", mktime(0, 0, 0, $this->month, 1, $this->year)); } private function getymd(){ echo "<form method='post'><th colspan='2'><select name='year' onchange='this.form.submit()'>"; for($i=$this->start;$i<=$this->stop;$i++){ if (strcmp($i, $this->year) == 0) { $select = "selected style='background-color:#c0c0c0'"; } else { $select = ""; } echo "<option value='".$i."' $select>".$i."</option>"; } echo "</select></th>"; echo "<th colspan='3'>".$this->year."年".$this->month."月".date("d",time()+3600*8)."</th>"; echo "<th colspan='2'><select name='month' onchange='this.form.submit()'>"; foreach($this->months as $k=>$v){ if (strcmp($k, $this->month) == 0) { $select = "selected style='background-color:#c0c0c0'"; } else { $select = ""; } echo "<option value='".$k."' $select>".$v."</option>"; } echo "</select></th></form>"; } private function getweek(){ echo "<tr>"; foreach($this->weeks as $v){ echo "<td class='week'>".$v."</td>"; } echo "<tr>"; } private function getdates(){ echo "<tr>"; for($i=0; $i<$this->day_week; $i++){ echo "<td> </td>"; } for($j=1; $j <= date("t", mktime(0, 0, 0, $this->month, 1, $this->year)); $j++){ $i++; if($j == date("d")){ echo "<td style='background:red'>".$j."</td>"; }else{ echo "<td style='background:green'>".$j."</td>"; } if($i%7 == 0){ echo "</tr>"; } } while($i%7 != 0){ echo "<td> </td>"; $i++; } } function run(){ echo "<table>"; $this->getymd(); $this->getweek(); $this->getdates(); echo "</table>"; } } $d = new Calendar(); $d->run(); ?>