网站制作学习网PHP→正文:日期类
字体:

日期类

PHP 2010/12/4 17:56:39  点击:不统计

学习www.网for站asp制.cn作

通过类来进行时间的操作,默认的是当前日期戳时间,可以通过不同时间格式输入来输出不同时间格式,以及判断星期几
<?
/*
*@author luhuijie
*时间类
*date:2010-12-3
*/
class time_{
private $_time;
private $_y;
private $_m;
private $_d;
private $_h;
private $_mi;
private $_s;
private $weekname=array("日","一","二","三","四","五","六");
var $err_message;
  function __get($name){
  return $this->$name;
  }
  function __set($name,$value){
  $this->$name = $value;
  }
  function __construct(){
  $this->_time =time();
  $this->int_date($this->_time);
  }
  function str_date($date){//格式是1908-09-08 12:00:01 或者是int数字日期格式。
    $timeall = explode(" ",trim($date));
    $temp = explode("-",$timeall[0]);
    $this->_y = ((int)$temp[0]);
    $this->_m = strlen($temp[1])>1?$temp[1]:"0".$temp[1];
    $this->_d = strlen($temp[2])>1?$temp[2]:"0".$temp[2];
    if(!checkdate($this->_m,$this->_d,$this->_y)){$this->set_err("日期错误!");return false;}
    if(count($timeall)>1){
    $temp2 = explode(":",$timeall[1]);
    $this->_h = $temp2[0];
    $this->_mi = $temp2[1];
    $this->_s = $temp2[2];
    }else{
    $this->_h = 12;
    $this->_mi = 0;
    $this->_s = 0;
    }
    $this->_time = mktime($this->_h,$this->_mi,$this->_s,$this->_m,$this->_d,$this->_y);
    return true;
  }

  function get_week(){
  echo "星期".$this->weekname[date("w",$this->_time)];
  }
  function get_intdate(){
   echo $this->_time;
  }//
 function get_date($type){
  switch($type){
  case 1:
   echo $this->_y."年".$this->_m."月".$this->_d."日".$this->_h."时".$this->_mi."分".$this->_s."秒";
     break;
  case 2:
  echo $this->_y."年".$this->_m."月".$this->_d."日";
     break;
  case 3:
  echo $this->_h."时".$this->_mi."分".$this->_s."秒";
     break;
  default:
   echo $this->_y."-".$this->_m."-".$this->_d;
     break;
  }
 }
 function int_date($date){
   $this->_time = $date;
   return $this->str_date(date("Y-m-d h:i:s",$this->_time));
 }
 private function set_err($err_message){
  $this->err_message = $err_message;
  }
}


$forasp = new time_();//默认为当前时间
$forasp->get_date(1);//2010年12月04日05时48分06秒
$forasp->get_week();//星期六
$forasp->get_intdate();//输出时间int日期戳形式 1291412886
$forasp->int_date("1291326468");
$forasp->get_week();//星期五
$forasp->get_date(1);//2010年12月03日05时47分48秒
$forasp->str_date("2010-12-02 5:47:48");
$forasp->get_week();//星期四
$forasp->get_intdate();//输出int日期戳格式1291240068
?>


·上一篇:php真伪静态 >>    ·下一篇:php读取动态生成静态页面 >>
推荐文章
最新文章