日历

<style type="text/css" media="screen"> 
<!-- /* table细线表格 */ 
    table { 
        border-collapse:collapse;     /* 关键属性:合并表格内外边框(其实表格边框有2px,外面1px,里面还有1px哦) */ 
        border:solid #999;             /* 设置边框属性;样式(solid=实线)、颜色(#999=灰) */ 
        border-1px 0 0 1px;     /* 设置边框状粗细:上 右 下 左 = 对应:1px 0 0 1px */ 
    } 
    table th,table td {border:solid #999;border-0 1px 1px 0;padding:2px;} 
    table td {text-align:center;} 
--> 
</style>
<?php
    /**
    * PHP万年历
    * @author Tank 2015/7/12
    */
    class Calendar{
    protected $_table;                    //table表格
    protected $_currentDate;            //当前日期
    protected $_year;                     //
    protected $_month;                     //
    protected $_days;                     //给定的月份应有的天数
    protected $_dayofweek;                //给定月份的 1号 是星期几
    protected $_preday;                    //获取上个月对应月的天数
    /**
    * 构造函数
    */
    public function __construct()
    {
        $this->_table="";
        // 获取日期信息年和月(默认为当前的年和当前月)
        $this->_year = isset($_GET["y"]) ? $_GET["y"] : date("Y");
        $this->_month = isset($_GET["m"]) ? $_GET["m"] : date("m");
        if ($this->_month>12){                //处理出现月份大于12的情况
            $this->_month=1;
            $this->_year++;
        }
        if ($this->_month<1){                //处理出现月份小于1的情况
            $this->_month=12;
            $this->_year--;
        }
        $this->_currentDate = $this->_year.'Year'.$this->_month.'Month';            //当前得到的日期信息
        $this->_days = date("t",mktime(0,0,0,$this->_month,1,$this->_year));        //得到给定的月份应有的天数
        $this->_dayofweek = date("w",mktime(0,0,0,$this->_month,1,$this->_year));    //得到给定的月份的 1号 是星期几
        $this->_preday = date("t",mktime(0,0,0,$this->_month,1,$this->_year));        //获取上个月对应月的天数
        $this->_nextday = date("t",mktime(0,0,0,$this->_month,1,$this->_year));        //获取下个个月对应月的天数
        $this->_nextw  = date("w",mktime(0,0,0,$this->_month+1,1,$this->_year));    //获取下个月中1号是星期几
    }
    /**
    * 输出标题和表头信息
    */
    protected function _showTitle()
    {
        $this->_table="<table><thead><tr align='center'><th colspan='7'>".$this->_currentDate."</th></tr></thead>";
        $this->_table.="<tbody><tr>";
        $this->_table .="<td style='color:red'>Sunday</td>";
        $this->_table .="<td>Monday</td>";
        $this->_table .="<td>Tuesday</td>";
        $this->_table .="<td>Wednesday</td>";
        $this->_table .="<td>Thursday</td>";
        $this->_table .="<td>Friday</td>";
        $this->_table .="<td style='color:red'>Saturday</td>";
        $this->_table.="</tr>";
    }
    /**
    * 输出日期信息
    * 根据当前日期输出日期信息
    */
    protected function _showDate()
    {
        $row = 1;                        //输出的行数
        $nums = $this->_dayofweek + 1;    //输出当月天数计数
        // var_dump($this->_dayofweek);
        
        //输出1号之前的空白日期
        for ($i=1;$i<=$this->_dayofweek;$i++){        
            $this->_table.='<td style="background :#eaeaea">'.($this->_preday - $this->_dayofweek + $i).'</td>';
        }
        // echo $this->_table;exit;
        //输出天数信息
        for ($i=1;$i<=$this->_days;$i++){
            //判断当月当天的背景颜色
            // echo date('d').'--'.$i.'---'.date('m').'---'.$this->_month.'<br>';
            if(date('d')==$i && date('m')==$this->_month){
                $backgroundColor = '#3870f9';
                $color = '#fff;';
            }else{
                $backgroundColor = '';
                $color = '';
            }
            
            if ($nums%7==0){//换行处理:7个一行
                $this->_table.="<td style='background:{$backgroundColor};'>$i</td></tr><tr>";
                $row++;
            }else{
                $this->_table.="<td style='background:{$backgroundColor};'>$i</td>";
            }
            $nums++;
        }
        
        // 输出下一个月的天数
        for ($k = 1;$k < $this->_nextday+1;$k++){
            if($row<=6){
                if ($nums%7==0){//换行处理:7个一行
                    $this->_table.='<td style="background :#eaeaea">'.($k).'</td></tr><tr>';
                    $row++;
                }else{
                    $this->_table.='<td style="background :#eaeaea">'.$k.'</td>';
                }
                $nums++;
            }
        }
        $this->_table.="</tr></tbody></table>";
        $this->_table.="<h3><a href='?y=".($this->_year)."&m=".($this->_month-1)."'>Last Mon</a>   ";
        $this->_table.="<a href='?y=".($this->_year)."&m=".($this->_month+1)."'>Next Mon</a></h3>";
    }
    /**
    * 输出日历
    */
    public function showCalendar()
    {
    $this->_showTitle();
    $this->_showDate();
    echo $this->_table;
    }
    }
    
    $calc=new Calendar();
    $calc->showCalendar();
?>
原文地址:https://www.cnblogs.com/phpfensi/p/4643050.html