当前位置:编程学习 > php >>

两个日期类

答案:<?
/**
  这是公历和农历类的定义,由于php的日期计算限制,所以只能计算1970-1938之间的时间
  农历类的计算方法使用了林洵贤先生的算法,在此表示感谢!在joy Asp可以找到林先生的大作(javascript)
*/

/**
* 日期类
* 本对象套用javascript的日期对象的方法
* 设置$mode属性,可兼容javascript日期对象
*/
class Date {
  var $time = 0;
  var $mode = 0;  // 本属性为与javascript兼容而设,$mode=1为javascript方式
  var $datemode = "Y-m-d H:i:s";  // 日期格式 "Y-m-d H:i:s",可自行设定
  function Date($t=0) {
    if($t == 0)
      $this->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;
    }
  &nb

上一个:php中的时间处理
下一个:初识PEAR

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,