欢迎光临
我们一直在努力

原创处理时间的类,请大家指正-PHP教程,PHP应用

建站超值云服务器,限时71元/月

timer.class.php
<?
//###################### start introduce #######################################
// author: bluemaple ; email: bluemaple@x263.net
// 最后修改时间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 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>";
?>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 原创处理时间的类,请大家指正-PHP教程,PHP应用
分享到: 更多 (0)