PHP程序编写时间控件类

11 篇文章 0 订阅
<?php 
header("Content-type:text/html;charset=utf-8");
    class calendar{  
        private $year,$month,$day;  
        private $arrWeek=array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');  
        private $days;//月应有的天数  
        private $arrMonth=array('1'=>'一月',  
            '2'=>'二月',  
            '3'=>'三月',  
            '4'=>'四月',  
            '5'=>'五月',  
            '6'=>'六月',  
            '7'=>'七月',  
            '8'=>'八月',  
            '9'=>'九月',  
            '10'=>'十月',  
            '11'=>'十一月',  
            '12'=>'十二月',);  
        /*设置年、月、日*/  
        private function setYear($year){  
            $this->year=$year;  
        }  
        private function setMonth($month){  
            $this->month=$month;  
        }  
        private function setDay($day){  
            $this->day=$day;  
        }  
        /*获取年、月、日*/  
        private function getYear(){  
            return $this->year;  
        }  
        private function getMonth(){  
            return $this->month;  
        }  
        private function getDay(){  
            return $this->day;  
        }  
        private function getDays($year,$month,$date=1){  
                while(checkdate($month,$date,$year)){  
                    $date++;  
                }  
                return $date;  
        }  
        /*显示当前日期、或表单提交的日期*/  
        public function showDate(){  
                $this->_env();  
                $nowday=$this->getWeek($this->getYear(),$this->getMonth(),$this->getDay());  
                $firstday=$this->getWeek($this->getYear(),$this->getMonth(),1);  
                $this->days=$this->getDays($this->getYear(),$this->getMonth());  
                echo "<div style=\"text-align:center;font:9px;width:236;margin:0px 15px\"><form action=$_SERVER[PHP_SELF] method=post  style=\"\" name=calenderform id=calenderform>";  
                echo "<select name=month id=month οnchange=\" submit();\">";  
                for($tmpi=1;$tmpi<13;$tmpi++){  
                    if($tmpi==$this->month){  
                        $select="selected style=\"background-color: #FFD700\"";  
                    }else{  
                        $select='';  
                    }  
                    echo "<option value=$tmpi $select>".$this->arrMonth[$tmpi]."</option>";  
                }  
                echo "</select><select name=year id=year οnchange=\" submit();\">";  
                for($tmpj=$this->year-15;$tmpj<=$this->year+15 && $tmpj<2038;$tmpj++){  
                    if($tmpj==$this->year){  
                        $select="selected style=\"background-color: #FFD700\"";  
                    }else{  
                        $select='';  
                    }  
                    echo "<option value=$tmpj $select>".$tmpj."</option>";  
                }  
                echo "</select>";  
                echo "<table align=center><tr>";  
                for($tmpi=0;$tmpi<count($this->arrWeek);$tmpi++){  
                    echo "<td width=29>".$this->arrWeek[$tmpi]."</td>";  
                }  
                echo "</tr>";  
                $tmpj=1;  
                while($tmpj < $this->days){  
                    if($tmpj==$this->day){  
                        $bgcolor='bgcolor=#FFD700';  
                    }else{  
                        $bgcolor='bgcolor=#FFE4B5';  
                    }  
                    if($tmpj==1){  
                     echo "<tr>";  
                      for($tmp=0;$tmp<$firstday;$tmp++){  
                        echo "<td ></td>";  
                      }  
                    }  
                    if($this->getWeek($this->getYear(),$this->getMonth(),$tmpj)==0){  
                        echo "</tr><tr><td $bgcolor>$tmpj</td>";  
                    }else{  
                        echo "<td $bgcolor>$tmpj</td>";  
                    }  
                    $tmpj++;  
                    }  
                    /*if($this->getWeek($this->getYear(),$this->getMonth(),$this->days-1)!=6){  
                        for($i=$this->getWeek($this->getYear(),$this->getMonth(),$this->days-1);$i<=6;$i++){  
                            echo "<td $bgcolor></td>";  
                        }  
                    }*/  
                echo "</tr></table></div>";  
            }  
                  
        /*默设置认为系统的时间也可以自己设置时间*/  
        private function _env(){  
            if(!isset($_POST['year'])){  
                $this->setYear(date('Y'));  
            }else{  
                $this->setYear($_POST['year']);  
            }  
            if(!isset($_POST['month'])){  
                $this->setMonth(date('m'));  
            }else{  
                $this->setMonth($_POST['month']);  
            }  
            $this->setDay(date('d'));  
        }  
        /*获取星期*/  
        private function getWeek($year,$month,$day){  
            $strap=strtotime("$year-$month-$day");  
            return date('w',$strap);  
        }  
    }  
    $calendar=new calendar();  
    $calendar->showDate();  
?> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值