處理時間的類

timer.class.php
<?
//###################### Start Introduce #######################################
// author: bluemaple ; email: [email protected]
// 最後修改時間2002-1-28 1:35
// 此函數求解決返回時間顯示格式問題。包括date()函數的所有格式,默認的$type爲最常用的類型
// 除了$year,$month,$day,$hour,$minute,$second;添加了$week(周),$zone(一年中的第幾天),$numMonth(當前月份的天數)
// 其中默認的都爲最常用的格式
// 特點,在時間處理中用得最多的是mktime,這裏設置mktime可以按照習慣輸入(年,月,日)顯示
// mktimeY();mktimeW();mktimeM();mktimeD();可以方便設置一個時間相隔y年,n月,在mysql檢索中方便使用
// subTime();函數可以方便求得兩個時間相差的天數,周等
//####################### End Introduce ########################################

class TIMER{
  var $year;   // 年
  var $month;  // 月
  var $day;    // 日
  var $hour;   // 時
  var $minute; // 分
  var $second; // 秒
  var $week;   // 周
  var $zone;   // 一年中的第幾天
  var $numMonth; // 當前月份的天數
  var $mktime;  // mktime
    
  function year($time="",$type=0){ // 返回年
                                   // $type=0表示返回四位的年份
                                   // $type=1表示返回二位的年份
      if($time=="") $time=time();
      if($type==0) $this->year=date("Y",$time);
      if($type==1) $this->year=date("y",$time);
      return $this->year;
       }   
     
  function month($time="",$type=0){ // 返回月
                                    // $type=0表示返回1~12
                                    // $type=1表示返回01~12
                                    // $type=2表示返回jan..三個英文字母
                                    // $type=3表示返回英語全名
      if($time=="") $time=time();
      if($type==0) $this->month=date("n",$time);
      if($type==1) $this->month=date("m",$time);
      if($type==2) $this->month=date("M",$time);
      if($type==3) $this->month=date("F",$time);
      return $this->month;
      }  
     
  function day($time="",$type=0){ // 返回日
                                  // $type=0返回1~31
                                  // $type=1返回01~31
      if($time=="") $time=time();
      if($type==0) $this->day=date("j",$time);
      if($type==1) $this->day=date("d",$time);
      return $this->day;
      }   

  function hour($time="",$type=0){ // 返回時
                                   // $type=0返回1~24
                                   // $type=1返回1~12
                                   // $type=2返回01~24
                                   // $type=3返回01~12
      if($time=="") $time=time();
      if($type==0) $this->hour=date("H",$time);
      if($type==1) $this->hour=date("h",$time);
      if($type==2) $this->hour=date("G",$time);
      if($type==3) $this->hour=date("g",$time);
      return $this->hour;
      }   
     
  function minute($time="",$type=0){ // 返回分
      if($time=="") $time=time();
      if($type==0) $this->minute=date("i",$time);
      return $this->minute;
    }
  
  function second($time="",$type=0){ // 返回秒
                                     // $type=0 返回1~59
                                     // $type=1 返回字尾加英文序數,二個英文字母
      if($time=="") $time=time();
      if($type==0) $this->second=date("s",$time);
      if($type==1) $this->second=date("S",$time);
      return $this->second;
      }
 
  function week($time="",$type=0){ // 返回周
                                   // $type=0 返回0~6
                                   // $type=1 返回三個字母的周
                                   // $type=2 返回全字母的周
      if($time=="") $time=time();
      if($type==0) $this->week=date("w",$time);
      if($type==1) $this->week=date("D",$time);
      if($type==2) $this->week=date("l",$time);
      return $this->week;
      }
 
  function zone($time=""){ // 一年中的第幾天;
        if($time=="") $time=time();
      $this->zone=date("z",$time);
      return $this->zone;
      }
 
  function numMonth($time=""){ // 當前月的天數
      if($time=="") $time=time();
      $this->numMonth=date("t",$time);
      return $this->numMonth;
      }

  function time($time=""){ //取得所有關於當前時間的參數。
      if($time=="") $time=time();
      $this->year($time);
      $this->month($time);
      $this->day($time);
      $this->hour($time);
      $this->minute($time);
      $this->second($time);
      $this->week($time);
      $this->zone($time);
      $this->numMonth($time);
      } 
 
  function betweenTime($aTime="",$bTime=""){ // 計算兩個時間相隔的天數,月數等
      if($aTime=="") $aTime=time();
      if($bTime=="") $bTime=time();
     
      }
  
  function mktime($year=0,$month=0,$day=0,$hour=0,$minute=0,$second=0){ // 年月日時分秒
      $this->mktime=mktime($hour,$minute,$second,$month,$day, $year);
      return $this->mktime;
      }
 
  function mktimeY($time="",$y=1){ // 取得某一時間y年以前的,默認爲1
      $this->time($time);
      $this->mktime=mktime(0,0,0,$this->month,$this->day,($this->year-$y));
      return $this->mktime;
      }
 
  function mktimeM($time="",$m=1){ // 取得某一時間m月以前的,默認爲1
      $this->time($time);
      $this->mktime=mktime(0,0,0,$this->month-$m,$this->day,$this->year);
      return $this->mktime;
      }
 
  function mktimeD($time="",$d=1){ // 取得某一時間d天以前的,默認爲1天
      $this->time($time);
      $this->mktime=mktime(0,0,0,$this->month,$this->day-$d,$this->year);
      return $this->mktime;
      }
 
  function mktimeW($time="",$w=1){ // 取得某一時間w個周以前的,默認爲1周
      $this->time($time);
      $this->mktime=mktime(0,0,0,$this->month,$this->day-7*$w,$this->year);
      return $this->mktime;
      }

  function subTime($aTime="",$bTime=""){ // 兩個時間之差,後者減去前者
      if($aTime=="") $aTime = time();
      if($bTime=="") $bTime = time();
      $subTime = $bTime - $aTime;
      $this->second=intval($subTime);
      $this->minute=intval($subTime/60);
      $this->hour=intval($this->minute/60);
      $this->day=intval($this->hour/24);
      $this->week=intval($this->day/7);
      $this->month=intval($this->day/30);
      $this->year=intval($this->monday/12);
      }
}
?>
測試text.php
<?
require("./timer.class.php");
//###################################
echo "<br>___________________________________<br>";
$TIMER=new TIMER;
$d=$TIMER->mktimeW();
$TIMER->subTime($d);
echo "second";echo $TIMER->second;echo "<br>";
echo "minute";echo $TIMER->minute;echo "<br>";
echo "hour";echo $TIMER->hour;echo "<br>";
echo "day";echo $TIMER->day;echo "<br>";
echo "week";echo $TIMER->week;echo "<br>";
echo "month";echo $TIMER->month;echo "<br>";
echo "year";echo $TIMER->year;echo "<br>";
echo "<br>___________________________________<br>";
?>

發佈了36 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章