首页 > 代码库 > PHP15-日历类实现

PHP15-日历类实现

日历类实现

1、输出星期

技术分享

calendar.class.php

 1 <?php 
 2     class Calendar{
 3 
 4         function out(){//输出表格
 5             echo ‘<table align="center">‘;
 6             $this->weeksList();        //输出星期
 7         8             echo ‘</table>‘;
 9         }
10         private function weeksList(){
11             
12             $week=array(‘日‘,‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘);
13             echo ‘<tr>‘;
14             for($i=0;$i<count($week);$i++)
15                 echo ‘<th class="fontb">‘.$week[$i].‘</th>‘;
16             echo ‘</tr>‘;
17         }
18         19 20 ?>

index.php

 1 <style>            //样式
 2     table {
 3         border:1px solid #050;    //边框,050:绿色
 4     }
 5     .fontb {
 6         color:white;
 7         background:blue;
 8     }
 9 
10     th {
11         width=30px;height=30px;
12     }
13 </style>
14 <?php
15     include "calendar.class.php";
16 
17     $calendar=new Calendar;
18     $calendar->out();
19 ?

 

2、输出日期

技术分享

calendar.class.php

技术分享
 1 <?php 
 2     class Calendar{
 3         private $year;
 4         private $month;
 5         private $start_weekday;    //    当月的第一天是周几
 6         private $days;//当前月有几天
 7 
 8         function __construct(){
 9             $this->year=date("Y");
10             $this->month=date("m");
11 
12             $this->start_weekday=date("w",mktime(0,0,0,$this->month,1,$this->year));
13             $this->days=date("t",mktime(0,0,0,$this->month,1,$this->year));
14         }
15 
16         function out(){//输出表格
17             echo ‘<table align="center">‘;
18             $this->weeksList();
19             $this->daysList();
20             echo ‘</table>‘;
21         }
22         private function weeksList(){
23             
24             $week=array(‘日‘,‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘);
25             echo ‘<tr>‘;
26             for($i=0;$i<count($week);$i++)
27                 echo ‘<th class="fontb">‘.$week[$i].‘</th>‘;
28             echo ‘</tr>‘;
29         }
30         private function daysList(){
31             echo ‘<tr>‘;
32             for($j=0;$j<$this->start_weekday;$j++)
33                 echo ‘<td>&nbsp;</td>‘;        //输出空格
34 
35             for($k=1;$k<=$this->days;$k++){
36                 $j++;
37                 if($k==date(‘d‘))        //当前天显示颜色
38                     echo ‘<td class="fontb">‘.$k.‘</td>‘;
39                 else 
40                     echo ‘<td>‘.$k.‘</td>‘;
41                 if ($j%7==0)
42                     echo ‘</tr><tr>‘;
43             }
44             echo ‘</tr>‘;
45         }
46     }
47 ?>
View Code

 

test.php

技术分享
 1 <style>
 2     table {
 3         border:1px solid #050;    
 4     }
 5     .fontb {
 6         color:white;
 7         background:blue;
 8     }
 9 
10     th {
11         width=30px;
12     }
13     td,th {            //使显示居中
14         height=30px;
15         text-align:center;
16     }
17 </style>
18 <?php
19     include "calendar.class.php";
20 
21     $calendar=new Calendar;
22     $calendar->out();
23 ?>
View Code

 

3、

技术分享

calendar.class.php

技术分享
  1 <?php 
  2     class Calendar{
  3         private $year;
  4         private $month;
  5         private $start_weekday;    //    当月的第一天是周几
  6         private $days;//当前月有几天
  7 
  8         function __construct(){
  9             $this->year=isset($_GET["year"])?htmlspecialchars($_GET["year"]):date("Y");
 10             $this->month=isset($_GET["month"])?htmlspecialchars($_GET["month"]):date("m");
 11 
 12             $this->start_weekday=date("w",mktime(0,0,0,$this->month,1,$this->year));
 13             $this->days=date("t",mktime(0,0,0,$this->month,1,$this->year));
 14         }
 15 
 16         function out(){//输出表格
 17             echo ‘<table align="center">‘;
 18             $this->chageDate();
 19             $this->weeksList();
 20             $this->daysList();
 21             echo ‘</table>‘;
 22         }
 23 
 24         private function weeksList(){
 25             
 26             $week=array(‘日‘,‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘);
 27             echo ‘<tr>‘;
 28             for($i=0;$i<count($week);$i++)
 29                 echo ‘<th class="fontb">‘.$week[$i].‘</th>‘;
 30             echo ‘</tr>‘;
 31         }
 32 
 33         private function daysList(){
 34             echo ‘<tr>‘;
 35             for($j=0;$j<$this->start_weekday;$j++)
 36                 echo ‘<td>&nbsp;</td>‘;        //输出空格
 37 
 38             for($k=1;$k<=$this->days;$k++){
 39                 $j++;
 40                 if($k==date(‘d‘))        //当前天显示颜色
 41                     echo ‘<td class="fontb">‘.$k.‘</td>‘;
 42                 else 
 43                     echo ‘<td>‘.$k.‘</td>‘;
 44                 if ($j%7==0)
 45                     echo ‘</tr><tr>‘;
 46             }
 47 
 48             //后面几个空格
 49             while($j%7!==0){
 50                 echo ‘<td>&nbsp;</td>‘;
 51                 $j++;
 52             }
 53             echo ‘</tr>‘;
 54         }
 55 
 56         private function prevYear($year,$month){
 57             $year=$year-1;
 58             if($year<1970)
 59                 $year=1970;
 60             return "year={$year}&month={$month}";
 61         }
 62         private function prevMonth($year,$month){
 63             if($month==1){
 64                 $year=$year-1;
 65                 $month=12;
 66                 if($year<1970)
 67                     $year=1970;
 68             }else{
 69                 $month--;
 70             }
 71             
 72             return "year={$year}&month={$month}";
 73         }
 74         private function nextYear($year,$month){
 75             $year=$year+1;
 76             if($year>2038)
 77                 $year=2038;
 78             return "year={$year}&month={$month}";
 79         }
 80         private function nextMonth($year,$month){
 81             if($month=12){
 82                 $year=$year+1;
 83                 $month=1;
 84                 if($year>2038)
 85                     $year=2038;
 86             }else{
 87                 $month++;
 88             }
 89             return "year={$year}&month={$month}";
 90         }
 91             
 92         private function chageDate(){
 93             echo ‘<tr>‘;
 94             echo ‘<td><a href="http://www.mamicode.com/?‘.$this->prevYear($this->year,$this->month).‘">‘.‘<<‘.‘</a></td>‘;
 95             echo ‘<td><a href="http://www.mamicode.com/?‘.$this->prevMonth($this->year,$this->month).‘">‘.‘<‘.‘</a></td>‘;
 96             echo ‘<td colspan="3">‘.$this->year.‘年‘.$this->month.‘月‘.‘</td>‘;
 97             echo ‘<td><a href="http://www.mamicode.com/?‘.$this->nextMonth($this->year,$this->month).‘">‘.‘>‘.‘</a></td>‘;
 98             echo ‘<td><a href="http://www.mamicode.com/?‘.$this->nextYear($this->year,$this->month).‘">‘.‘>>‘.‘</a></td>‘;
 99             echo ‘</tr>‘;
100         }
101     }
102 ?>
View Code

 

4、增加年月选择下拉列表  *

calendar.calss.php

技术分享
<?php 
    class Calendar{
        private $year;
        private $month;
        private $start_weekday;    //    当月的第一天是周几
        private $days;//当前月有几天

        function __construct(){
            $this->year=isset($_GET["year"])?htmlspecialchars($_GET["year"]):date("Y");
            $this->month=isset($_GET["month"])?htmlspecialchars($_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->chageDate("text.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>&nbsp;</td>‘;        //输出空格

            for($k=1;$k<=$this->days;$k++){
                $j++;
                if($k==date(‘d‘))        //当前天显示颜色
                    echo ‘<td class="fontb">‘.$k.‘</td>‘;
                else 
                    echo ‘<td>‘.$k.‘</td>‘;
                if ($j%7==0)
                    echo ‘</tr><tr>‘;
            }

            //后面几个空格
            while($j%7!==0){
                echo ‘<td>&nbsp;</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;
                $month=12;
                if($year<1970)
                    $year=1970;
            }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;
                $month=1;
                if($year>2038)
                    $year=2038;
            }else{
                $month++;
            }
            return "year={$year}&month={$month}";
        }
            
        private function chageDate($url=""){
            echo ‘<tr>‘;
            echo ‘<td><a href="http://www.mamicode.com/?‘.$this->prevYear($this->year,$this->month).‘">‘.‘<<‘.‘</a></td>‘;
            echo ‘<td><a href="http://www.mamicode.com/?‘.$this->prevMonth($this->year,$this->month).‘">‘.‘<‘.‘</a></td>‘;
            
            echo ‘<td colspan="3">‘;
            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="http://www.mamicode.com/‘.$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="http://www.mamicode.com/‘.$sm.‘">‘.$sm.‘</option>‘;
            }
            echo ‘</select>‘;
            echo ‘</form>‘;
            echo ‘</td>‘;
            
            echo ‘<td><a href="http://www.mamicode.com/?‘.$this->nextMonth($this->year,$this->month).‘">‘.‘>‘.‘</a></td>‘;
            echo ‘<td><a href="http://www.mamicode.com/?‘.$this->nextYear($this->year,$this->month).‘">‘.‘>>‘.‘</a></td>‘;
            echo ‘</tr>‘;
        }
    }
?>
View Code

test.php

技术分享
<style>
    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>
<?php
    include "calendar.class.php";

    $calendar=new Calendar;
    $calendar->out();
?>
View Code

 

PHP15-日历类实现