文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>两个日期类

两个日期类

时间:2008-04-18  来源:剑心通明


time = time();
  }
  /**
   * 返回从gmt时间1970年1月1日0时开始的毫秒数
   */
  function gettime() {
    $temp = gettimeofday();
    return $temp[sec]*1000+round($temp[usec]/1000);
  }
  /**
   * 返回年份
   */
  function getyear() {
    $temp = getdate($this->time);
    return $temp[year];
  }
  /**
   * 返回月份
   */
  function getmonth() {
    $temp = getdate($this->time);
    return $temp[mon]-$this->mode;
  }
  /**
   * 返回日期
   */
  function getdate() {
    $temp = getdate($this->time);
    return $temp[mday];
  }
  /**
   * 返回星期
   */
  function getday() {
    $temp = getdate($this->time);
    return $temp[wday]-$this->mode;
  }
  /**
   * 返回小时
   */
  function gethours() {
    $temp = getdate($this->time);
    return $temp[hours];
  }
  /**
   * 返回分
   */
  function getminutes() {
    $temp = getdate($this->time);
    return $temp[minutes];
  }
  /**
   * 返回秒
   */
  function getseconds() {
    $temp = getdate($this->time);
    return $temp[seconds];
  }
  /**
   * 设定年份
   * php 4.0.6 year 1970 -- 2038
   */
  function setyear($val) {
    $temp = getdate($this->time);
    $temp[year] = $val;
    $this->set_time($temp);
  }
  /**
   * 设定月份
   */
  function setmonth($val) {
    $temp = getdate($this->time);
    $temp[mon] = $val+$this->mode;
    $this->set_time($temp);
  }
  /**
   * 设定日期
   */
  function setdate($val) {
    $temp = getdate($this->time);
    $temp[mday] = $val;
    $this->set_time($temp);
  }
  /**
   * 设定星期
   */
  function setday($val) {
    $temp = getdate($this->time);
    $temp[wday] = $val+$this->mode;
    $this->set_time($temp);
  }
  /**
   * 设定小时
   */
  function sethours($val) {
    $temp = getdate($this->time);
    $temp[hours] = $val;
    $this->set_time($temp);
  }
  /**
   * 设定分
   */
  function setminutes($val) {
    $temp = getdate($this->time);
    $temp[minutes] = $val;
    $this->set_time($temp);
  }
  /**
   * 设定秒
   */
  function setseconds($val) {
    $temp = getdate($this->time);
    $temp[seconds] = $val;
    $this->set_time($temp);
  }
  /**
   * 返回系统格式的字符串
   */
  function tolocalestring() {
    return date($this->datemode,$this->time);
  }
  /**
   * 使用gtm时间创建一个日期值
   */
  function utc($year,$mon,$mday,$hours=0,$minutes=0,$seconds=0) {
    $this->time = mktime($hours,$minutes,$seconds,$mon,$mday,$year);
    return $this->time;
  }
  /**
   * 等价于dateadd(interval,number,date)
   * 返回已添加指定时间间隔的日期。
   * inetrval为表示要添加的时间间隔字符串表达式,例如分或天
   * number为表示要添加的时间间隔的个数的数值表达式
   * date表示日期
   *
   * interval(时间间隔字符串表达式)可以是以下任意值:
   *  yyyy year年
   *  q quarter季度
   *  m month月
   *  y day of year一年的数
   *  d day天
   *  w weekday一周的天数
   *  ww week of year周
   *  h hour小时
   *  n minute分
   *  s second秒
   *  w、y和d的作用是完全一样的,即在目前的日期上加一天,q加3个月,ww加7天。
   */
  function add($interval, $number, $date) {
    $date = date::get_time($date);
    $date_time_array = getdate($date);
    $hours = $date_time_array["hours"];
    $minutes = $date_time_array["minutes"];
    $seconds = $date_time_array["seconds"];
    $month = $date_time_array["mon"];
    $day = $date_time_array["mday"];
    $year = $date_time_array["year"];
    switch ($interval) {
      case "yyyy": $year +=$number; break;
      case "q": $month +=($number*3); break;
      case "m": $month +=$number; break;
      case "y":
      case "d":
      case "w": $day+=$number; break;
      case "ww": $day+=($number*7); break;
      case "h": $hours+=$number; break;
      case "n": $minutes+=$number; break;
      case "s": $seconds+=$number; break;
    }
    $temptime = mktime($hours ,$minutes, $seconds,$month ,$day, $year);
    return $temptime;
  }
  /**
   * 等价于datediff(interval,date1,date2)
   * 返回两个日期之间的时间间隔
   * intervals(时间间隔字符串表达式)可以是以下任意值:
   *   w  周
   *   d  天
   *   h  小时
   *   n  分钟
   *   s  秒
   */
  function diff($interval, $date1,$date2) {
    // 得到两日期之间间隔的秒数
    $timedifference = date::get_time($date2) - date::get_time($date1);
    switch ($interval) {
      case "w": $retval = bcdiv($timedifference ,604800); break;
      case "d": $retval = bcdiv( $timedifference,86400); break;
      case "h": $retval = bcdiv ($timedifference,3600); break;
      case "n": $retval = bcdiv( $timedifference,60); break;
      case "s": $retval = $timedifference; break;
    }
    return $retval;
  }
  /**
   * 输出,根据需要直接修改本函数或在派生类中重写本函数
   */
  function display() {
    $nstr = array(日,一,二,三,四,五,六);
    echo sprintf("%4d年%2d月%2d日 星期%s
",$this->getyear(),$this->getmonth(),$this->getdate(),$nstr[$this->getday()%7]);
  }
  /**
   *
[url=javascript:;]工作[/url]
函数
   */
  function set_time(&$ar) {
    $this->time = mktime($ar[hours],$ar[minutes],$ar[seconds],$ar[mon],$ar[mday],$ar[year]);
  }
  /**
   * 转换为unix时间戳
   */
  function get_time($d) {
    if(is_numeric($d))
      return $d;
    else {
      if(! is_string($d)) return 0;
      if(ereg(":",$d)) {
        $buf = split(" +",$d);
        $year = split("[-/]",$buf[0]);
        $hour = split(":",$buf[1]);
        if(eregi("pm",$buf[2]))
          $hour[0] += 12;
        return mktime($hour[0],$hour[1],$hour[2],$year[1],$year[2],$year[0]);
      }else {
        $year = split("[-/]",$d);
        return mktime(0,0,0,$year[1],$year[2],$year[0]);
      }
    }
  }
} // 日期类定义结束
?>
0x8; $i>>=1)
      $sum += ($this->lunarinfo[$y-1900] & $i)? 1: 0;
    return $sum+$this->leapdays($y);
  }
  /**
   * 传回农历 y年闰月的天数
   */
  function leapdays($y) {
    if($this->leapmonth($y))
      return ($this->lunarinfo[$y-1900] & 0x10000)? 30: 29;
    else return 0;
  }
  /**
   * 传回农历 y年闰哪个月 1-12 , 没闰传回 0
   */
  function leapmonth($y) {
    return $this->lunarinfo[$y-1900] & 0xf;
  }
  /**
   * 传回农历 y年m月的总天数
   */
  function monthdays($y,$m) {
    return ($this->lunarinfo[$y-1900] & (0x10000>>$m))? 30: 29;
  }
  /**
   * 创建农历日期对象
   */
  function lunar($objdate,$month=1,$day=1) {
    $leap=0;
    $temp=0;
    if(is_object($objdate))
      $this->time = mktime(0,0,0,$objdate->getmonth(),$objdate->getdate(),$objdate->getyear());
    else {
      $year = $objdate;
      $this->time = mktime(0,0,0,$month,$day,$year);
      if($year $year; $i--) {
          $temp = $this->lyeardays($i);
          $offset -= $temp;
        }
      }
    }
    $offset = round($this->time/86400+25537);
    $this->daycyl = $offset + 40;
    $this->moncyl = 14;
    for($i=1900; $i0; $i++) {
      $temp = $this->lyeardays($i);
      $offset -= $temp;
      $this->moncyl += 12;
    }
    if($offsetmoncyl -= 12;
    }
    $this->year = $i;
    $this->yearcyl = $i-1864;
    $leap = $this->leapmonth($i); //闰哪个月
    $this->isleap = false;
    for($i=1; $i0; $i++) {
      //闰月
      if($leap>0 && $i==($leap+1) && $this->isleap==false) {
        $i--;
        $this->isleap = true;
        $temp = $this->leapdays($this->year);
      }else {
        $temp = $this->monthdays($this->year, $i);
      }
      //解除闰月
      if($this->isleap==true && $i==($leap+1))
        $this->isleap = false;
      $offset -= $temp;
      if($this->isleap == false)
        $this->moncyl ++;
    }
    if($offset==0 && $leap>0 && $i==$leap+1)
      if($this->isleap)
        $this->isleap = false;
      else {
        $this->isleap = true;
        $i--;
        $this->moncyl--;
      }
    if($offsetmoncyl--;
    }
    $this->month = $i;
    $this->day = $offset + 1;
  }
  function cyclical($num) {
    $gan = array("甲","乙","丙","丁","戊","己","庚","辛","壬","癸");
    $zhi = array("子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥");
    return $gan[$num%10].$zhi[$num%12];
  }
  /**
   * 输出,根据需要直接修改本函数或在派生类中重写本函数
   */
  function display() {
    $nstr = array( ,正,二,三,四,五,六,七,八,九,十,十一,腊);
    echo sprintf("农历 %s%s月%s
",($this->isleap?"闰":""),$nstr[$this->month],$this->cday($this->day));
    echo sprintf("%s年 %s月 %s日",$this->cyclical($this->yearcyl),$this->cyclical($this->moncyl),$this->cyclical($this->daycyl));
  }
  /**
   * 中文日期
   */
  function cday($d) {
    $nstr1 = array(日,一,二,三,四,五,六,七,八,九,十);
    $nstr2 = array(初,十,廿,卅, );
    switch($d) {
      case 10:
        $s = 初十;
        break;
      case 20:
        $s = 二十;
        break;
      case 30:
        $s = 三十;
        break;
      default :
        $s = $nstr2[floor($d/10)];
        $s .= $nstr1[$d%10];
    }
    return $s;
  }
}  // 农历类定义结束
?>
display();
$d->setyear(2037);
echo "
现在是:";
echo $d->getyear()."年";
echo $d->getmonth()."月";
echo $d->getdate()."日";
echo " 星期".$d->getday()." ";
echo $d->gethours()."时";
echo $d->getminutes()."分";
echo $d->getseconds()."秒
";
$d->utc(1998,10,2,22,0,8);
echo $d->tolocalestring()."
";
$d = new date;
$ld = new lunar($d);
$d->display();
$ld->display();
$d->utc(1998,10,2,22,0,8);
$ld = new lunar($d);
$d->display();
$ld->display();
$ld = new lunar(2002,2,12);
$ld->display();
echo __line__."
";
$d1 = "2002-01-11";
$d2 = date("y-m-d",$d->add("d",35,$d1));
echo $d1."的". date::diff("d",$d1,$d2)."天后是$d2
";
echo $d1."的10天前是".date("y-m-d",date::add("d",-10,$d1))."
";
$d3 = date("y/m/d h:i:s");
echo "现在是".$d3."距离2002/2/12 12:59:59还有".date::diff("s",$d3,"2002/2/12 12:59:59")."秒
";
?>


相关阅读 更多 +
排行榜 更多 +
西安交大通

西安交大通

生活实用 下载
长江云通

长江云通

生活实用 下载
translatez

translatez

生活实用 下载