php操作date的类

今天花了点时间写了个date类,与大家共享,希望可以为你带来帮助,也希望你为写的更好….

set_day($date);
     }
     /*
      * 判断是否是时间
      */
     function is_date($date){
     	$pattern = '/[\d]{4}-[\d]{1,2}-[\d]{1,2}/';
     	return preg_match($pattern,$date);
     }
     function set_date($date){
     	if(is_date($date)){
     		list($y,$m,$d) = explode("-",$date);
     		$this->set_year($y);
     		$this->set_month($m);
     		$this->set_day($d);
     	}else{
            $this->year(date("Y"));
            $this->month(date("m"));
     		$this->day($date("d"));
     	}

     }
    function is_leapyear($year){
    	return date("L",$year);
    }
    /*
     * 设置年数
     */
     function set_year($year){
     	$year = ltrim(intval($year),"0");//rtrim — 删除字符串末端的空白字符(或者其他字符)
     	$this->year = ($year<=2100 && $year>=1970) ? $year : date('Y');
     }
     /*
      * 设置月
      */
      function set_month($month){
      	$month = ltrim(intval($month),"0");//rtrim — 删除字符串末端的空白字符(或者其他字符)
      	$this->month = ($month<13 && $month>0) ? $month : date('d');
      }
      /*
       * 设置天数
      */
      function set_day($day){
      	$day = ltrim(intval($day),"0");//rtrim — 删除字符串末端的空白字符(或者其他字符)
      	$this->day = ($this->year && $this->month && checkdate($this->month, $day, $this->year)) ? $day : date('d');//checkdate -- 验证一个格里高里日期
     }
    /*
     * 获取当前月的第一天
     */
     function get_firstDay(){
     	return 1;
     }
    /*
     * 获取一月的最后一天
     */
     function get_lastDay(){
     	if($this->month==2)
		{
			$lastday = $this->is_leapyear($this->year)?29:28;
		}elseif($this->month==4 ||$this->month==6 ||$this->month==6 ||$this->month==9  ){
            $lastday = 30;
		}else{
			$lastday = 31;
		}
		return $lastday;
     }
     /*
      * 获取当前的年份
      */
      function getYear(){
      	return $this->year;
      }
      /*
       * 获取当前月份
       */
       function getMonth(){
       	  return $this->month;
       }
       /*
        * 获取当前天份
        */
        function getDay(){
        	return $this->day;
        }
       /*
        * 当前星期几
        */
        function getWeek(){
        	return date("w",$this->getTime());
        }
        /*
         * 获取时间,返回当前Unix 时间戳
         */
         function getTime(){
         	return strtotime($this->get_date().' 23:59:59');
         }

}
?>
is_date($time);
echo $is_time."
"; $day = $date->getDay($time); echo $day."
"; $lastDay = $date->get_lastDay($time); echo $lastDay."
"; ?>

发表评论

返回顶部