日期:2011-05-07  浏览次数:20403 次


<?
/**
  这是公历和农历类的定义,由于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);
  }
  /**