PHP设计日历类一

全屏阅读
  • 基本信息
  • 作者:
  • 作者已发布:924篇文章
  • 发布时间:2021年03月05日 20:33:39
  • 所属分类:PHP+MySql
  • 阅读次数:1953次阅读
  • 标签:

由两个文件组成:

第一个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();

第二个日历类: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->chageDate("test.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> </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> </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;
        
                if($year < 1970)
                    $year = 1970;

                $month=12;
            }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++;

                if($year > 2038)
                    $year=2038;

                $month=1;
            }else{
                $month++;
            }
            
            return "year={$year}&month={$month}";    
        }

        private function chageDate($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).'">'.'<'.'</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="'.$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="'.$sm.'">'.$sm.'</option>';
            }
            echo '</select>';
            echo '</form>';    
            echo '</td>';

            echo '<td><a href="?'.$this->nextYear($this->year, $this->month).'">'.'>>'.'</a></td>';
            echo '<td><a href="?'.$this->nextMonth($this->year, $this->month).'">'.'>'.'</a></td>';
            echo '</tr>';
        }

    }

效果如下000000000000000.png

顶一下
(0)
100%
订阅 回复
踩一下
(0)
100%
» 固定链接:恒富网 » 《PHP设计日历类一》
» 郑重声明:本文由mpxq168发布,所有内容仅代表个人观点。版权归恒富网mpxq168共有,欢迎转载, 但未经作者同意必须保留此段声明,并给出文章连接,否则保留追究法律责任的权利! 如果本文侵犯了您的权益,请留言。

目前有 0 条留言 其中:访客:0 条, 博主:0 条

给我留言

您必须 [ 登录 ] 才能发表留言!