php时间类

testl.php

<style>
   table{
      border:1px solid #050;
   border-collapse:collapse;
   }
   .fontb{
      color:white;
   background:blue;
   }  
   th{
       width:30px;
    height:30px;
    text-align:center;
   }
   td{
       height:30px;
    text-align:center;
   }
   form{
       margin:0px;
    padding:0px;
   }
</style>
<?php
    include "calendarl.class.php";
    $calendar=new Calendar;
 $calendar->out();
?>

calendar.class.php

<?php
class Calendar{
   private $year;//当前的年
   private $month;//当前的月
   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->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("testl.php");
    $this->weeksList();
    $this->daysList();
    echo '</table>';
   }
   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=12;
    }else{
        $month--;
    }
       return "year={$year}&month={$month}";
   }
    private function nextMonth($year,$month){
    if($month==12){
     $year++;
     $month=1;
     if($year>2038){
      $year=2038;
     }
    }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 changeDate($url=""){
       echo '<tr>';
    echo '<td><a href="?'.$this->prevYear($this->year,$this->month).'">'.'<<'.'</a></td>';
    echo '<td><a href="?'.$this->prevMonth($this->year,$this->month).'">'.'<'.'</td>';
    echo '<td colspan="3">';
    echo '<form>';
    echo '<select name="year" onchange="window.location=\''.$url.'?year=\'+this.options[selectedIndex].value"+\'&month='.$this->month.'\'">';
    for($i=1970;$i<2039;$i++){
     $selected=($i==$this->year)?selected:"";
     echo '<option '.$selected.' value="'.$i.'">'.$i.'</option>';
    }
    echo '</select>';
    echo '<select name="month" onchange="window.location=\''.$url.'?year='.$this->year.'&month=\'+this.options[selectedIndex].value">';
    for($i=1;$i<13;$i++){
     $selected1=($i==$this->month)?selected:"";
     echo '<option '.$selected1.' value="'.$i.'">'.$i.'</option>';
    }
    echo '</select>';
    echo '</form>';
    echo '</td>';
    echo '<td><a href="?'.$this->nextMonth($this->year,$this->month).'">'.'>'.'</td>';
    echo '<td><a href="?'.$this->nextYear($this->year,$this->month).'">'.'>>'.'</td>';
    echo '</tr>';
   }
   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($k=0;$k<$this->start_weekday;$k++){
        echo '<td>&nbsp;</td>';
    }
    //开始输出日期
       for($i=1;$i<=$this->days;$i++){
     if($i==date('d')){
       echo '<td class="fontb">'.$i.'</td>';
     }else{
       echo '<td>'.$i.'</td>';
     }
     $k++;
     if($k%7==0){
      echo '</tr>';
      echo '<tr>';
     }
    //判断什么时候换行,自己的方法
    /*if(date(w,mktime(0,0,0,$this->month,$i,$this->year))==6){
          echo '</tr>';
    echo '<tr>';
       } */ 
    }
    while($k%7!=0){
     echo '<td>&nbsp;</td>';
     $k++;
    }
       echo '</tr>';
   }
}
?>

posted @ 2013-03-14 18:30  达也.  阅读(121)  评论(0)    收藏  举报