【PHP】日曆Demo

樣品圖片:


地址:http://xhua521.cn/DemoFolder/calendar/index.php


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 __tostring(){
    $out = '<table align=center>';
    $out .= $this->chageDate();
    $out .= $this->weeksList();
    $out .= $this->daysList();
    
    $out .= '</table>';
    // $out .= $this->year.'--'.$this->month;
    return $out;
  }

  //星期列表
  public function weeksList()
  {
    # code...
    $week = array('日','一','二','三','四','五','六');
    $out .= "<tr>";
    for ($i=0; $i < count($week); $i++) { 
      # code...
      $out .= '<th class=fontb>'.$week[$i].'</th>';
    }
    return $out;
  }
  
  //日期列表
  public function daysList()
  {
    # code...
    $out .= '<tr>';
    for ($j=0; $j < $this->start_weekday; $j++) { 
      $out .= '<td> </td>';
    }
    
    for ($k=1; $k <= $this->days; $k++) { 
      $j++;
      if($k == date('d')){
        $out .= '<td class="fontb">'.$k.'</td>';
      }else {
        $out .= '<td>'.$k.'</td>';
      }
      if ($j%7 == 0) {
        $out .= '</tr><tr>';
      }
    }
    while($j%7 !== 0){
      $out .= '<td> </td>';
      $j++;
    }
    $out .= '</td>';
    return $out;
  }

  //獲取上一年
  public function prevYear($year,$month)
  {
    $year = $year-1;
    if ($year<1970) {
      $year = 1970;
    }
    return "year=$year&month=$month";
  }
  
  //獲取上一月
  public function prevMonth($year,$month)
  {
    if ($month==1) {
      # code...
      $year = $year-1;
      if ($year<1970) {
        $year = 1970;
      }
      $month = 12;
    }else {
      $month--;
    }
    return "year=$year&month=$month";
  }
  
  //獲取下一年的日期
  public function nextYear($year,$month)
  {
    $year = $year+1;
    if ($year>2038){
      $year = 2038;
    }
    return "year=$year&month=$month";
  }
  
  //獲取下一月的日期
  public function nextMonth($year,$month)
  {
    if ($month==12) {
      $year = $year+1;
      if($year>2038){
        $year = 2038;
      }
      $month = 1;
    }else {
      $month++;
    }
    return "year=$year&month=$month";
  }
  
  //上一年,上一月、下一月、下一年、年月列表
  public function chageDate($url='index.php')
  {
    // $out .= "<tr>";
    $out .= "<tr><td><a href=".$url.'?'.$this->prevYear($this->year,$this->month)."><<</a></td>";
    $out .= "<td><a href=".$url.'?'.$this->prevMonth($this->year,$this->month)."><</a></td>";
    // $out .= '<td colspan="3">';
    $out .= '<td colspan="3"><select name="year" οnchange="window.location=\''.$url.'?year=\'+this.options[selectedIndex].value+\'&month='.$this->month.'\'">';
    for ($sy=1970; $sy <= 2038; $sy++) { 
        $selected = ($sy==$this->year)? 'selected' : '';
        $out .= '<option '.$selected.' value='.$sy.'>'.$sy.'</option>';
    }
    // $out .= "</select>";
    $out .= '</select><select name="month" οnchange="window.location=\''.$url.'?year='.$this->year.'&month=\'+this.options[selectedIndex].value">';
    for ($sm=1; $sm <=12 ; $sm++) { 
        $selected = ($sm==$this->month) ? 'selected' : '';
        $out .= '<option '.$selected.' value='.$sm.'>'.$sm.'</option>';
    }
    $out .= "</select></form></td>";
    // $out .= "</form>";
    // $out .= "</td>";
    $out .= "<td><a href=".$url.'?'.$this->nextMonth($this->year,$this->month).">></a></td>";
    $out .= "<td><a href=".$url.'?'.$this->nextYear($this->year,$this->month).">>></a></td>";
    return $out;
  }
}
?>



index.php文件

作用:調用日曆類,顯示日期

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style media="screen">
    table{
      border: 1px solid #050;
    }
    .fontb{
      color:white;
      background:blue;
    }
    th{
      width:30px;
    }
    td,th{
      height: 30px;
      text-align: center;
    }
    form{
      margin: 0px;
      padding:0px;
    }
  </style>
</head>
<body>
<?php  
  require "./calendar.class.php";
  ini_set("error_reporting","E_ALL & ~E_NOTICE");
  echo new Calendar();
?>
</body>
</html>


calendar.class.php日曆類。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章