<?php
class calendar{
private $week;
private $year;
private $month;
private $week_first_day;
private $month_all_day;
private $a;
public function __construct(){
$this->week = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六');
$this->year = isset($_REQUEST['year'])?$_REQUEST['year']:date('Y');
$this->month = isset($_REQUEST['month'])?$_REQUEST['month']:date('m');
$this->a = strtotime($this->year.'-'.$this->month.'-1');
$this->week_first_day = date('w',$this->a);
$this->month_all_day = date('t',$this->a);
}
public function out(){
echo '<meta charset="utf-8" />';
echo '<title>日历实例</title>';
echo '<link rel="shortcut icon" href="icon.jpg" type="image/x-icon" />';
echo "<table style='width:500px;text-align:center'>";
echo "<tr>";
echo "<td colspan=2><a href='".$this->prevYear()."'><<上一年</a><a href='".$this->prevMonth()."'><上一月</a></td><td colspan=3><h2>日 历</h2><p>".$this->year."年".$this->month."月</p></td><td colspan=2><a href='".$this->nextMonth()."'>下一月></a><a href='".$this->nextYear()."'>下一年>></a></td>";
echo "</tr>";
$this->week();
$this->day();
$this->formPost();
echo "</table>";
}
public function day(){
echo "<tr>";
for ($i=0; $i < $this->week_first_day; $i++) {
echo '<td></td>';
}
for ($j=0; $j < $this->month_all_day; $j++) {
echo '<td>'.($j+1).'日</td>';
if(($j+1+$i)%7 == 0){
echo "</tr><tr>";
}
}
echo "</tr>";
}
public function nextYear(){
$year = $this->year + 1;
$url = "?year=".$year."&month=".$this->month."";
return $url;
}
public function addYear($year){
$year = $year + 1;
return $year;
}
public function cutYear($year){
$year = $year - 1;
return $year;
}
public function nextMonth(){
if( $this->month > 11 ){
$month = 1;
$url = "?year=".$this->addYear($this->year)."&month=".$month."";
return $url;
}
$month = $this->month + 1;
$url = "?year=".$this->year."&month=".$month."";
return $url;
}
public function prevYear(){
$year = $this->year - 1;
$url = "?year=".$year."&month=".$this->month."";
return $url;
}
public function prevMonth(){
if( $this->month < 2 ){
$month = 12;
$url = "?year=".$this->cutYear($this->year)."&month=".$month."";
return $url;
}
$month = $this->month - 1;
$url = "?year=".$this->year."&month=".$month."";
return $url;
}
public function week(){
echo "<tr>";
for ($i=0; $i < count($this->week); $i++) {
echo '<td style="border:red 1px solid;">';
echo $this->week[$i];
echo '</td>';
}
echo "</tr>";
}
public function formPost(){
echo "<tr>";
echo "<td colspan=7><form action='' method='post' name='form'>";
echo "<select name='year' onchange='document.form.submit()'>";
for($i=date('Y');$i>=1970;$i--){
echo "<option value='".$i."'>".$i."年</option>";
}
echo "</select>";
echo "<select name='month' onchange='document.form.submit()'>";
for($j=1;$j<=12;$j++){
echo "<option value='".$j."'>".$j."月</option>";
}
echo "</select>";
echo "</form></td>";
echo "</tr>";
}
}
$calendar = new calendar;
$calendar->out();
?>