[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/data/ -> Date.class.php (source)

   1  <?php
   2  
   3  /* vim: set expandtab tabstop=4 shiftwidth=4: */
   4  // +----------------------------------------------------------------------+
   5  // | PHP Version 4                                                        |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 1997-2003 The PHP Group                                |
   8  // +----------------------------------------------------------------------+
   9  // | This source file is subject to version 2.02 of the PHP license,      |
  10  // | that is bundled with this package in the file LICENSE, and is        |
  11  // | available at through the world-wide-web at                           |
  12  // | http://www.php.net/license/2_02.txt.                                 |
  13  // | If you did not receive a copy of the PHP license and are unable to   |
  14  // | obtain it through the world-wide-web, please send a note to          |
  15  // | license@php.net so we can mail you a copy immediately.               |
  16  // +----------------------------------------------------------------------+
  17  // | Authors: Baba Buehler <baba@babaz.com>                               |
  18  // |          Pierre-Alain Joye <pajoye@php.net>                          |
  19  // +----------------------------------------------------------------------+
  20  //
  21  // $Id: Date.php,v 1.28 2004/05/16 13:11:07 pajoye Exp $
  22  
  23  /**@#+
  24   * Include supporting classes
  25   */
  26  // require_once PLOG_CLASS_PATH.'class/data/Date/TimeZone.class.php';
  27  lt_include(PLOG_CLASS_PATH.'class/data/Date/Calc.class.php');
  28  lt_include(PLOG_CLASS_PATH.'class/data/Date/Span.class.php');
  29  lt_include(PLOG_CLASS_PATH.'class/data/Date/TimeZone.class.php');
  30  /**@#-*/
  31  
  32  /**@#+
  33   * Output formats.  Pass this to getDate().
  34   */
  35  /**
  36   * "YYYY-MM-DD HH:MM:SS"
  37   */
  38  define('DATE_FORMAT_ISO', 1);
  39  /**
  40   * "YYYYMMSSTHHMMSS(Z|(+/-)HHMM)?"
  41   */
  42  define('DATE_FORMAT_ISO_BASIC', 2);
  43  /**
  44   * "YYYY-MM-SSTHH:MM:SS(Z|(+/-)HH:MM)?"
  45   */
  46  define('DATE_FORMAT_ISO_EXTENDED', 3);
  47  /**
  48   * "YYYY-MM-SSTHH:MM:SS(.S*)?(Z|(+/-)HH:MM)?"
  49   */
  50  define('DATE_FORMAT_ISO_EXTENDED_MICROTIME', 6);
  51  /**
  52   * "YYYYMMDDHHMMSS"
  53   */
  54  define('DATE_FORMAT_TIMESTAMP', 4);
  55  /**
  56   * long int, seconds since the unix epoch
  57   */
  58  define('DATE_FORMAT_UNIXTIME', 5);
  59  /**@#-*/
  60  
  61  /**
  62   * \ingroup Data
  63   *
  64   * Generic date handling class for PEAR.
  65   *
  66   * Generic date handling class for PEAR.  Attempts to be time zone aware
  67   * through the Date::TimeZone class.  Supports several operations from
  68   * Date::Calc on Date objects.
  69   *
  70   * @author Baba Buehler <baba@babaz.com>
  71   * @access public
  72   */
  73  class Date
  74  {
  75      /**
  76       * the year
  77       * @var int
  78       */
  79      var $year;
  80      /**
  81       * the month
  82       * @var int
  83       */
  84      var $month;
  85      /**
  86       * the day
  87       * @var int
  88       */
  89      var $day;
  90      /**
  91       * the hour
  92       * @var int
  93       */
  94      var $hour;
  95      /**
  96       * the minute
  97       * @var int
  98       */
  99      var $minute;
 100      /**
 101       * the second
 102       * @var int
 103       */
 104      var $second;
 105      /**
 106       * the parts of a second
 107       * @var float
 108       */
 109      var $partsecond;
 110      /**
 111       * timezone for this date
 112       * @var object Date_TimeZone
 113       */
 114      var $tz;
 115  
 116      var $offset = '+ 00:00';
 117  
 118  
 119      /**
 120       * Constructor
 121       *
 122       * Creates a new Date Object initialized to the current date/time in the
 123       * system-default timezone by default.  A date optionally
 124       * passed in may be in the ISO 8601, TIMESTAMP or UNIXTIME format,
 125       * or another Date object.  If no date is passed, the current date/time
 126       * is used.
 127       *
 128       * @access public
 129       * @see setDate()
 130       * @param mixed $date optional - date/time to initialize
 131       * @return object Date the new Date object
 132       */
 133      function Date($date = null)
 134      {
 135          $this->tz = Date_TimeZone::getDefault();
 136          if (is_null($date)) {
 137              $this->setDate(date("Y-m-d H:i:s"));
 138          } elseif (is_a($date, 'Date')) {
 139              $this->copy($date);
 140          } else {
 141              $this->setDate($date);
 142          }
 143      }
 144  
 145      /**
 146       * Set the fields of a Date object based on the input date and format
 147       *
 148       * Set the fields of a Date object based on the input date and format,
 149       * which is specified by the DATE_FORMAT_* constants.
 150       *
 151       * @access public
 152       * @param string $date input date
 153       * @param int $format Optional format constant (DATE_FORMAT_*) of the input date.
 154       *                    This parameter isn't really needed anymore, but you could
 155       *                    use it to force DATE_FORMAT_UNIXTIME.
 156       */
 157      function setDate($date, $format = DATE_FORMAT_ISO)
 158      {
 159  
 160          if (
 161              preg_match('/^(\d{4})-?(\d{2})-?(\d{2})([T\s]?(\d{2}):?(\d{2}):?(\d{2})(\.\d+)?(Z|[\+\-]\d{2}:?\d{2})?)?$/i', $date, $regs)
 162              && $format != DATE_FORMAT_UNIXTIME) {
 163              // DATE_FORMAT_ISO, ISO_BASIC, ISO_EXTENDED, and TIMESTAMP
 164              // These formats are extremely close to each other.  This regex
 165              // is very loose and accepts almost any butchered format you could
 166              // throw at it.  e.g. 2003-10-07 19:45:15 and 2003-10071945:15
 167              // are the same thing in the eyes of this regex, even though the
 168              // latter is not a valid ISO 8601 date.
 169              $this->year       = $regs[1];
 170              $this->month      = $regs[2];
 171              $this->day        = $regs[3];
 172              $this->hour       = isset($regs[5])?$regs[5]:0;
 173              $this->minute     = isset($regs[6])?$regs[6]:0;
 174              $this->second     = isset($regs[7])?$regs[7]:0;
 175              $this->partsecond = isset($regs[8])?(float)$regs[8]:(float)0;
 176  
 177              // if an offset is defined, convert time to UTC
 178              // Date currently can't set a timezone only by offset,
 179              // so it has to store it as UTC
 180              if (isset($regs[9])) {
 181                  $this->toUTCbyOffset($regs[9]);
 182                  $this->offset = $regs[9];
 183              }
 184          } elseif (is_numeric($date)) {
 185              // UNIXTIME
 186              $this->setDate(date("Y-m-d H:i:s", $date));
 187          } else {
 188              // unknown format
 189              $this->year       = 0;
 190              $this->month      = 1;
 191              $this->day        = 1;
 192              $this->hour       = 0;
 193              $this->minute     = 0;
 194              $this->second     = 0;
 195              $this->partsecond = (float)0;
 196          }
 197      }
 198  
 199      /**
 200       * Get a string (or other) representation of this date
 201       *
 202       * Get a string (or other) representation of this date in the
 203       * format specified by the DATE_FORMAT_* constants.
 204       *
 205       * @access public
 206       * @param int $format format constant (DATE_FORMAT_*) of the output date
 207       * @return string the date in the requested format
 208       */
 209      function getDate($format = DATE_FORMAT_ISO)
 210      {
 211          switch ($format) {
 212          case DATE_FORMAT_ISO:
 213              return $this->format("%Y-%m-%d %T");
 214              break;
 215          case DATE_FORMAT_ISO_BASIC:
 216              $format = "%Y%m%dT%H%M%S";
 217              //if ($this->tz->getID() == 'UTC') {
 218                  $format .= "Z";
 219              //}
 220              return $this->format($format);
 221              break;
 222          case DATE_FORMAT_ISO_EXTENDED:
 223              $format = "%Y-%m-%dT%H:%M:%S";
 224              //if ($this->tz->getID() == 'UTC') {
 225                  $format .= "Z";
 226              //}
 227              return $this->format($format);
 228              break;
 229          case DATE_FORMAT_ISO_EXTENDED_MICROTIME:
 230              $format = "%Y-%m-%dT%H:%M:%s";
 231              //if ($this->tz->getID() == 'UTC') {
 232                  $format .= "Z";
 233              //}
 234              return $this->format($format);
 235              break;
 236          case DATE_FORMAT_TIMESTAMP:
 237              return $this->format("%Y%m%d%H%M%S");
 238              break;
 239          case DATE_FORMAT_UNIXTIME:
 240              return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
 241              break;
 242          }
 243      }
 244  
 245      /**
 246       * Copy values from another Date object
 247       *
 248       * Makes this Date a copy of another Date object.
 249       *
 250       * @access public
 251       * @param object Date $date Date to copy from
 252       */
 253      function copy($date)
 254      {
 255          $this->year = $date->year;
 256          $this->month = $date->month;
 257          $this->day = $date->day;
 258          $this->hour = $date->hour;
 259          $this->minute = $date->minute;
 260          $this->second = $date->second;
 261          $this->tz = $date->tz;
 262      }
 263  
 264      /**
 265       *  Date pretty printing, similar to strftime()
 266       *
 267       *  Formats the date in the given format, much like
 268       *  strftime().  Most strftime() options are supported.<br><br>
 269       *
 270       *  formatting options:<br><br>
 271       *
 272       *  <code>%a  </code>  abbreviated weekday name (Sun, Mon, Tue) <br>
 273       *  <code>%A  </code>  full weekday name (Sunday, Monday, Tuesday) <br>
 274       *  <code>%b  </code>  abbreviated month name (Jan, Feb, Mar) <br>
 275       *  <code>%B  </code>  full month name (January, February, March) <br>
 276       *  <code>%C  </code>  century number (the year divided by 100 and truncated to an integer, range 00 to 99) <br>
 277       *  <code>%d  </code>  day of month (range 00 to 31) <br>
 278       *  <code>%D  </code>  same as "%m/%d/%y" <br>
 279       *  <code>%e  </code>  day of month, single digit (range 0 to 31) <br>
 280       *  <code>%E  </code>  number of days since unspecified epoch (integer, Date_Calc::dateToDays()) <br>
 281       *  <code>%H  </code>  hour as decimal number (00 to 23) <br>
 282       *  <code>%I  </code>  hour as decimal number on 12-hour clock (01 to 12) <br>
 283       *  <code>%j  </code>  day of year (range 001 to 366) <br>
 284       *  <code>%m  </code>  month as decimal number (range 01 to 12) <br>
 285       *  <code>%M  </code>  minute as a decimal number (00 to 59) <br>
 286       *  <code>%n  </code>  newline character (\n) <br>
 287       *  <code>%O  </code>  dst-corrected timezone offset expressed as "+/-HH:MM" <br>
 288       *  <code>%o  </code>  raw timezone offset expressed as "+/-HH:MM" <br>
 289       *  <code>%p  </code>  either 'am' or 'pm' depending on the time <br>
 290       *  <code>%P  </code>  either 'AM' or 'PM' depending on the time <br>
 291       *  <code>%r  </code>  time in am/pm notation, same as "%I:%M:%S %p" <br>
 292       *  <code>%R  </code>  time in 24-hour notation, same as "%H:%M" <br>
 293       *  <code>%s  </code>  seconds including the decimal representation smaller than one second <br>
 294       *  <code>%S  </code>  seconds as a decimal number (00 to 59) <br>
 295       *  <code>%t  </code>  tab character (\t) <br>
 296       *  <code>%T  </code>  current time, same as "%H:%M:%S" <br>
 297       *  <code>%w  </code>  weekday as decimal (0 = Sunday) <br>
 298       *  <code>%U  </code>  week number of current year, first sunday as first week <br>
 299       *  <code>%y  </code>  year as decimal (range 00 to 99) <br>
 300       *  <code>%Y  </code>  year as decimal including century (range 0000 to 9999) <br>
 301       *  <code>%%  </code>  literal '%' <br>
 302       * <br>
 303       *
 304       * @access public
 305       * @param string format the format string for returned date/time
 306       * @return string date/time in given format
 307       */
 308      function format($format)
 309      {
 310          $output = "";
 311  
 312          for($strpos = 0; $strpos < strlen($format); $strpos++) {
 313              $char = substr($format,$strpos,1);
 314              if ($char == "%") {
 315                  $nextchar = substr($format,$strpos + 1,1);
 316                  switch ($nextchar) {
 317                  case "a":
 318                      $output .= Date_Calc::getWeekdayAbbrname($this->day,$this->month,$this->year);
 319                      break;
 320                  case "A":
 321                      $output .= Date_Calc::getWeekdayFullname($this->day,$this->month,$this->year);
 322                      break;
 323                  case "b":
 324                      $output .= Date_Calc::getMonthAbbrname($this->month);
 325                      break;
 326                  case "B":
 327                      $output .= Date_Calc::getMonthFullname($this->month);
 328                      break;
 329                  case "C":
 330                      $output .= sprintf("%02d",intval($this->year/100));
 331                      break;
 332                  case "d":
 333                      $output .= sprintf("%02d",$this->day);
 334                      break;
 335                  case "D":
 336                      $output .= sprintf("%02d/%02d/%02d",$this->month,$this->day,$this->year);
 337                      break;
 338                  case "e":
 339                      $output .= $this->day * 1; // get rid of leading zero
 340                      break;
 341                  case "E":
 342                      $output .= Date_Calc::dateToDays($this->day,$this->month,$this->year);
 343                      break;
 344                  case "H":
 345                      $output .= sprintf("%02d", $this->hour);
 346                      break;
 347                  case "I":
 348                      $hour = ($this->hour + 1) > 12 ? $this->hour - 12 : $this->hour;
 349                      $output .= sprintf("%02d", $hour==0 ? 12 : $hour);
 350                      break;
 351                  case "j":
 352                      $output .= Date_Calc::julianDate($this->day,$this->month,$this->year);
 353                      break;
 354                  case "m":
 355                      $output .= sprintf("%02d",$this->month);
 356                      break;
 357                  case "M":
 358                      $output .= sprintf("%02d",$this->minute);
 359                      break;
 360                  case "n":
 361                      $output .= "\n";
 362                      break;
 363                  case "O":
 364                      // :TODO: fix me .. this is just a hack right now, we need to calculate the correct offset
 365                      $output .= $this->offset;
 366                      break;
 367                      $offms = $this->tz->getOffset($this);
 368                      $direction = $offms >= 0 ? "+" : "-";
 369                      $offmins = abs($offms) / 1000 / 60;
 370                      $hours = $offmins / 60;
 371                      $minutes = $offmins % 60;
 372                      $output .= sprintf("%s%02d:%02d", $direction, $hours, $minutes);
 373                      break;
 374                  case "o":
 375                      $output = $this->offset;
 376                      break;
 377                      $offms = $this->tz->getRawOffset($this);
 378                      $direction = $offms >= 0 ? "+" : "-";
 379                      $offmins = abs($offms) / 1000 / 60;
 380                      $hours = $offmins / 60;
 381                      $minutes = $offmins % 60;
 382                      $output .= sprintf("%s%02d:%02d", $direction, $hours, $minutes);
 383                      break;
 384                  case "p":
 385                      $output .= $this->hour >= 12 ? "pm" : "am";
 386                      break;
 387                  case "P":
 388                      $output .= $this->hour >= 12 ? "PM" : "AM";
 389                      break;
 390                  case "r":
 391                      $hour = ($this->hour + 1) > 12 ? $this->hour - 12 : $this->hour;
 392                      $output .= sprintf("%02d:%02d:%02d %s", $hour==0 ?  12 : $hour, $this->minute, $this->second, $this->hour >= 12 ? "PM" : "AM");
 393                      break;
 394                  case "R":
 395                      $output .= sprintf("%02d:%02d", $this->hour, $this->minute);
 396                      break;
 397                  case "s":
 398                      $output .= sprintf("%02f", (float)((float)$this->second + $this->partsecond));
 399                      break;
 400                  case "S":
 401                      $output .= sprintf("%02d", $this->second);
 402                      break;
 403                  case "t":
 404                      $output .= "\t";
 405                      break;
 406                  case "T":
 407                      $output .= sprintf("%02d:%02d:%02d", $this->hour, $this->minute, $this->second);
 408                      break;
 409                  case "w":
 410                      $output .= Date_Calc::dayOfWeek($this->day,$this->month,$this->year);
 411                      break;
 412                  case "U":
 413                      $output .= Date_Calc::weekOfYear($this->day,$this->month,$this->year);
 414                      break;
 415                  case "y":
 416                      $output .= substr($this->year,2,2);
 417                      break;
 418                  case "Y":
 419                      $output .= $this->year;
 420                      break;
 421                  case "Z":
 422                      $output .= $this->tz->inDaylightTime($this) ? $this->tz->getDSTShortName() : $this->tz->getShortName();
 423                      break;
 424                  case "%":
 425                      $output .= "%";
 426                      break;
 427                  default:
 428                      $output .= $char.$nextchar;
 429                  }
 430                  $strpos++;
 431              } else {
 432                  $output .= $char;
 433              }
 434          }
 435          return $output;
 436  
 437      }
 438  
 439      /**
 440       * Get this date/time in Unix time() format
 441       *
 442       * Get a representation of this date in Unix time() format.  This may only be
 443       * valid for dates from 1970 to ~2038.
 444       *
 445       * @access public
 446       * @return int number of seconds since the unix epoch
 447       */
 448      function getTime()
 449      {
 450          return $this->getDate(DATE_FORMAT_UNIXTIME);
 451      }
 452  
 453      /**
 454       * Sets the time zone of this Date
 455       *
 456       * Sets the time zone of this date with the given
 457       * Date_TimeZone object.  Does not alter the date/time,
 458       * only assigns a new time zone.  For conversion, use
 459       * convertTZ().
 460       *
 461       * @access public
 462       * @param object Date_TimeZone $tz the Date_TimeZone object to use, if called 
 463       * with a paramater that is not a Date_TimeZone object, will fall through to
 464       * setTZbyID(). 
 465       */
 466      function setTZ($tz)
 467      {
 468          if(is_a($tz, 'Date_Timezone')) {
 469              $this->tz = $tz;
 470          } else {
 471              $this->setTZbyID($tz);
 472          }
 473      }
 474  
 475      /**
 476       * Sets the time zone of this date with the given time zone id
 477       *
 478       * Sets the time zone of this date with the given
 479       * time zone id, or to the system default if the
 480       * given id is invalid. Does not alter the date/time,
 481       * only assigns a new time zone.  For conversion, use
 482       * convertTZ().
 483       *
 484       * @access public
 485       * @param string id a time zone id
 486       */
 487      function setTZbyID($id)
 488      {
 489          if (Date_TimeZone::isValidID($id)) {
 490              $this->tz = new Date_TimeZone($id);
 491          } else {
 492              $this->tz = Date_TimeZone::getDefault();
 493          }
 494      }
 495  
 496      /**
 497       * Tests if this date/time is in DST
 498       *
 499       * Returns true if daylight savings time is in effect for
 500       * this date in this date's time zone.  See Date_TimeZone::inDaylightTime()
 501       * for compatability information.
 502       *
 503       * @access public
 504       * @return boolean true if DST is in effect for this date
 505       */
 506      function inDaylightTime()
 507      {
 508          return $this->tz->inDaylightTime($this);
 509      }
 510  
 511      /**
 512       * Converts this date to UTC and sets this date's timezone to UTC
 513       *
 514       * Converts this date to UTC and sets this date's timezone to UTC
 515       *
 516       * @access public
 517       */
 518      function toUTC()
 519      {
 520          if ($this->tz->getOffset($this) > 0) {
 521              $this->subtractSeconds(intval($this->tz->getOffset($this) / 1000));
 522          } else {
 523              $this->addSeconds(intval(abs($this->tz->getOffset($this)) / 1000));
 524          }
 525          // $this->tz = new Date_TimeZone('UTC');
 526      }
 527  
 528      /**
 529       * Converts this date to a new time zone
 530       *
 531       * Converts this date to a new time zone.
 532       * WARNING: This may not work correctly if your system does not allow
 533       * putenv() or if localtime() does not work in your environment.  See
 534       * Date::TimeZone::inDaylightTime() for more information.
 535       *
 536       * @access public
 537       * @param object Date_TimeZone $tz the Date::TimeZone object for the conversion time zone
 538       */
 539  //
 540  //    disabling TZ Features
 541  //     
 542  //    function convertTZ($tz)
 543  //    {
 544  //        // convert to UTC
 545  //        if ($this->tz->getOffset($this) > 0) {
 546  //            $this->subtractSeconds(intval(abs($this->tz->getOffset($this)) / 1000));
 547  //        } else {
 548  //            $this->addSeconds(intval(abs($this->tz->getOffset($this)) / 1000));
 549  //        }
 550  //        // convert UTC to new timezone
 551  //        if ($tz->getOffset($this) > 0) {
 552  //            $this->addSeconds(intval(abs($tz->getOffset($this)) / 1000));
 553  //        } else {
 554  //            $this->subtractSeconds(intval(abs($tz->getOffset($this)) / 1000));
 555  //        }
 556  //        $this->tz = $tz;
 557  //    }
 558  
 559      /**
 560       * Converts this date to a new time zone, given a valid time zone ID
 561       *
 562       * Converts this date to a new time zone, given a valid time zone ID
 563       * WARNING: This may not work correctly if your system does not allow
 564       * putenv() or if localtime() does not work in your environment.  See
 565       * Date::TimeZone::inDaylightTime() for more information.
 566       *
 567       * @access public
 568       * @param string id a time zone id
 569       */
 570  //    function convertTZbyID($id)
 571  //    {
 572  //       if (Date_TimeZone::isValidID($id)) {
 573  //          $tz = new Date_TimeZone($id);
 574  //       } else {
 575  //          $tz = Date_TimeZone::getDefault();
 576  //       }
 577  //       $this->convertTZ($tz);
 578  //    }
 579  
 580      function toUTCbyOffset($offset)
 581      {
 582          if ($offset == "Z" || $offset == "+00:00" || $offset == "+0000") {
 583              $this->toUTC();
 584              return true;
 585          }
 586  
 587          if (preg_match('/([\+\-])(\d{2}):?(\d{2})/', $offset, $regs)) {
 588              // convert offset to seconds
 589              $hours  = (int) isset($regs[2])?$regs[2]:0;
 590              $mins   = (int) isset($regs[3])?$regs[3]:0;
 591              $offset = ($hours * 3600) + ($mins * 60);
 592  
 593              if (isset($regs[1]) && $regs[1] == "-") {
 594                  $offset *= -1;
 595              }
 596  
 597              if ($offset > 0) {
 598                  $this->subtractSeconds(intval($offset));
 599              } else {
 600                  $this->addSeconds(intval(abs($offset)));
 601              }
 602  
 603              // $this->tz = new Date_TimeZone('UTC');
 604              return true;
 605          }
 606  
 607          return false;
 608      }
 609  
 610      /**
 611       * Adds a given number of seconds to the date
 612       *
 613       * Adds a given number of seconds to the date
 614       *
 615       * @access public
 616       * @param int $sec the number of seconds to add
 617       */
 618      function addSeconds($sec)
 619      {
 620          $this->addSpan(new Date_Span((integer)$sec));
 621      }
 622  
 623      /**
 624       * Adds a time span to the date
 625       *
 626       * Adds a time span to the date
 627       *
 628       * @access public
 629       * @param object Date_Span $span the time span to add
 630       */
 631      function addSpan($span)
 632      {
 633          if (!is_a($span, 'Date_Span')) {
 634              return;
 635          }
 636  
 637          $this->second += $span->second;
 638          if ($this->second >= 60) {
 639              $this->minute++;
 640              $this->second -= 60;
 641          }
 642  
 643          $this->minute += $span->minute;
 644          if ($this->minute >= 60) {
 645              $this->hour++;
 646              if ($this->hour >= 24) {
 647                  list($this->year, $this->month, $this->day) =
 648                      sscanf(Date_Calc::nextDay($this->day, $this->month, $this->year), "%04s%02s%02s");
 649                  $this->hour -= 24;
 650              }
 651              $this->minute -= 60;
 652          }
 653  
 654          $this->hour += $span->hour;
 655          if ($this->hour >= 24) {
 656              list($this->year, $this->month, $this->day) =
 657                  sscanf(Date_Calc::nextDay($this->day, $this->month, $this->year), "%04s%02s%02s");
 658              $this->hour -= 24;
 659          }
 660  
 661          $d = Date_Calc::dateToDays($this->day, $this->month, $this->year);
 662          $d += $span->day;
 663  
 664          list($this->year, $this->month, $this->day) =
 665              sscanf(Date_Calc::daysToDate($d), "%04s%02s%02s");
 666          $this->year  = intval($this->year);
 667          $this->month = intval($this->month);
 668          $this->day   = intval($this->day);
 669      }
 670  
 671      /**
 672       * Subtracts a given number of seconds from the date
 673       *
 674       * Subtracts a given number of seconds from the date
 675       *
 676       * @access public
 677       * @param int $sec the number of seconds to subtract
 678       */
 679      function subtractSeconds($sec)
 680      {
 681          $this->subtractSpan(new Date_Span($sec));
 682      }
 683  
 684      /**
 685       * Subtracts a time span to the date
 686       *
 687       * Subtracts a time span to the date
 688       *
 689       * @access public
 690       * @param object Date_Span $span the time span to subtract
 691       */
 692      function subtractSpan($span)
 693      {
 694          if (!is_a($span, 'Date_Span')) {
 695              return;
 696          }
 697          if ($span->isEmpty()) {
 698              return;
 699          }
 700  
 701          $this->second -= $span->second;
 702          if ($this->second < 0) {
 703              $this->minute--;
 704              $this->second += 60;
 705          }
 706  
 707          $this->minute -= $span->minute;
 708          if ($this->minute < 0) {
 709              $this->hour--;
 710              if ($this->hour < 0) {
 711                  list($this->year, $this->month, $this->day) =
 712                      sscanf(Date_Calc::prevDay($this->day, $this->month, $this->year), "%04s%02s%02s");
 713                  $this->hour += 24;
 714              }
 715              $this->minute += 60;
 716          }
 717  
 718          $this->hour -= $span->hour;
 719          if ($this->hour < 0) {
 720              list($this->year, $this->month, $this->day) =
 721                  sscanf(Date_Calc::prevDay($this->day, $this->month, $this->year), "%04s%02s%02s");
 722              $this->hour += 24;
 723          }
 724  
 725          $d = Date_Calc::dateToDays($this->day, $this->month, $this->year);
 726          $d -= $span->day;
 727  
 728          list($this->year, $this->month, $this->day) =
 729              sscanf(Date_Calc::daysToDate($d), "%04s%02s%02s");
 730          $this->year  = intval($this->year);
 731          $this->month = intval($this->month);
 732          $this->day   = intval($this->day);
 733      }
 734  
 735      /**
 736       * Compares two dates
 737       *
 738       * Compares two dates.  Suitable for use
 739       * in sorting functions.
 740       *
 741       * @access public
 742       * @param object Date $d1 the first date
 743       * @param object Date $d2 the second date
 744       * @return int 0 if the dates are equal, -1 if d1 is before d2, 1 if d1 is after d2
 745       */
 746      /*function compare($d1, $d2)
 747      {
 748          // $d1->convertTZ(new Date_TimeZone('UTC'));
 749          $d2->convertTZ(new Date_TimeZone('UTC'));
 750          $days1 = Date_Calc::dateToDays($d1->day, $d1->month, $d1->year);
 751          $days2 = Date_Calc::dateToDays($d2->day, $d2->month, $d2->year);
 752          if ($days1 < $days2) return -1;
 753          if ($days1 > $days2) return 1;
 754          if ($d1->hour < $d2->hour) return -1;
 755          if ($d1->hour > $d2->hour) return 1;
 756          if ($d1->minute < $d2->minute) return -1;
 757          if ($d1->minute > $d2->minute) return 1;
 758          if ($d1->second < $d2->second) return -1;
 759          if ($d1->second > $d2->second) return 1;
 760          return 0;
 761      }*/
 762  
 763      /**
 764       * Test if this date/time is before a certain date/time
 765       *
 766       * Test if this date/time is before a certain date/time
 767       *
 768       * @access public
 769       * @param object Date $when the date to test against
 770       * @return boolean true if this date is before $when
 771       */
 772      /*function before($when)
 773      {
 774          if (Date::compare($this,$when) == -1) {
 775              return true;
 776          } else {
 777              return false;
 778          }
 779      }*/
 780  
 781      /**
 782       * Test if this date/time is after a certian date/time
 783       *
 784       * Test if this date/time is after a certian date/time
 785       *
 786       * @access public
 787       * @param object Date $when the date to test against
 788       * @return boolean true if this date is after $when
 789       */
 790      /*function after($when)
 791      {
 792          if (Date::compare($this,$when) == 1) {
 793              return true;
 794          } else {
 795              return false;
 796          }
 797      }*/
 798  
 799      /**
 800       * Test if this date/time is exactly equal to a certian date/time
 801       *
 802       * Test if this date/time is exactly equal to a certian date/time
 803       *
 804       * @access public
 805       * @param object Date $when the date to test against
 806       * @return boolean true if this date is exactly equal to $when
 807       */
 808      /*function equals($when)
 809      {
 810          if (Date::compare($this,$when) == 0) {
 811              return true;
 812          } else {
 813              return false;
 814          }
 815      }*/
 816  
 817      /**
 818       * Determine if this date is in the future
 819       *
 820       * Determine if this date is in the future
 821       *
 822       * @access public
 823       * @return boolean true if this date is in the future
 824       */
 825      /*function isFuture()
 826      {
 827          $now = new Date();
 828          if ($this->after($now)) {
 829              return true;
 830          } else {
 831              return false;
 832          }
 833      }*/
 834  
 835      /**
 836       * Determine if this date is in the past
 837       *
 838       * Determine if this date is in the past
 839       *
 840       * @access public
 841       * @return boolean true if this date is in the past
 842       */
 843      /*function isPast()
 844      {
 845          $now = new Date();
 846          if ($this->before($now)) {
 847              return true;
 848          } else {
 849              return false;
 850          }
 851      }*/
 852  
 853      /**
 854       * Determine if the year in this date is a leap year
 855       *
 856       * Determine if the year in this date is a leap year
 857       *
 858       * @access public
 859       * @return boolean true if this year is a leap year
 860       */
 861      function isLeapYear()
 862      {
 863          return Date_Calc::isLeapYear($this->year);
 864      }
 865  
 866      /**
 867       * Get the Julian date for this date
 868       *
 869       * Get the Julian date for this date
 870       *
 871       * @access public
 872       * @return int the Julian date
 873       */
 874      function getJulianDate()
 875      {
 876          return Date_Calc::julianDate($this->day, $this->month, $this->year);
 877      }
 878  
 879      /**
 880       * Gets the day of the week for this date
 881       *
 882       * Gets the day of the week for this date (0=Sunday)
 883       *
 884       * @access public
 885       * @return int the day of the week (0=Sunday)
 886       */
 887      function getDayOfWeek()
 888      {
 889          return Date_Calc::dayOfWeek($this->day, $this->month, $this->year);
 890      }
 891  
 892      /**
 893       * Gets the week of the year for this date
 894       *
 895       * Gets the week of the year for this date
 896       *
 897       * @access public
 898       * @return int the week of the year
 899       */
 900      function getWeekOfYear()
 901      {
 902          return Date_Calc::weekOfYear($this->day, $this->month, $this->year);
 903      }
 904  
 905      /**
 906       * Gets the quarter of the year for this date
 907       *
 908       * Gets the quarter of the year for this date
 909       *
 910       * @access public
 911       * @return int the quarter of the year (1-4)
 912       */
 913      function getQuarterOfYear()
 914      {
 915          return Date_Calc::quarterOfYear($this->day, $this->month, $this->year);
 916      }
 917  
 918      /**
 919       * Gets number of days in the month for this date
 920       *
 921       * Gets number of days in the month for this date
 922       *
 923       * @access public
 924       * @return int number of days in this month
 925       */
 926      function getDaysInMonth()
 927      {
 928          return Date_Calc::daysInMonth($this->month, $this->year);
 929      }
 930  
 931      /**
 932       * Gets the number of weeks in the month for this date
 933       *
 934       * Gets the number of weeks in the month for this date
 935       *
 936       * @access public
 937       * @return int number of weeks in this month
 938       */
 939      function getWeeksInMonth()
 940      {
 941          return Date_Calc::weeksInMonth($this->month, $this->year);
 942      }
 943  
 944      /**
 945       * Gets the full name or abbriviated name of this weekday
 946       *
 947       * Gets the full name or abbriviated name of this weekday
 948       *
 949       * @access public
 950       * @param boolean $abbr abbrivate the name
 951       * @return string name of this day
 952       */
 953      function getDayName($abbr = false, $length = 3)
 954      {
 955          if ($abbr) {
 956              return Date_Calc::getWeekdayAbbrname($this->day, $this->month, $this->year, $length);
 957          } else {
 958              return Date_Calc::getWeekdayFullname($this->day, $this->month, $this->year);
 959          }
 960      }
 961  
 962      /**
 963       * Gets the full name or abbriviated name of this month
 964       *
 965       * Gets the full name or abbriviated name of this month
 966       *
 967       * @access public
 968       * @param boolean $abbr abbrivate the name
 969       * @return string name of this month
 970       */
 971      function getMonthName($abbr = false)
 972      {
 973          if ($abbr) {
 974              return Date_Calc::getMonthAbbrname($this->month);
 975          } else {
 976              return Date_Calc::getMonthFullname($this->month);
 977          }
 978      }
 979  
 980      /**
 981       * Get a Date object for the day after this one
 982       *
 983       * Get a Date object for the day after this one.
 984       * The time of the returned Date object is the same as this time.
 985       *
 986       * @access public
 987       * @return object Date Date representing the next day
 988       */
 989      function getNextDay()
 990      {
 991          $day = Date_Calc::nextDay($this->day, $this->month, $this->year, "%Y-%m-%d");
 992          $date = sprintf("%s %02d:%02d:%02d", $day, $this->hour, $this->minute, $this->second);
 993          $newDate = new Date();
 994          $newDate->setDate($date);
 995          return $newDate;
 996      }
 997  
 998      /**
 999       * Get a Date object for the day before this one
1000       *
1001       * Get a Date object for the day before this one.
1002       * The time of the returned Date object is the same as this time.
1003       *
1004       * @access public
1005       * @return object Date Date representing the previous day
1006       */
1007      function getPrevDay()
1008      {
1009          $day = Date_Calc::prevDay($this->day, $this->month, $this->year, "%Y-%m-%d");
1010          $date = sprintf("%s %02d:%02d:%02d", $day, $this->hour, $this->minute, $this->second);
1011          $newDate = new Date();
1012          $newDate->setDate($date);
1013          return $newDate;
1014      }
1015  
1016      /**
1017       * Get a Date object for the weekday after this one
1018       *
1019       * Get a Date object for the weekday after this one.
1020       * The time of the returned Date object is the same as this time.
1021       *
1022       * @access public
1023       * @return object Date Date representing the next weekday
1024       */
1025      function getNextWeekday()
1026      {
1027          $day = Date_Calc::nextWeekday($this->day, $this->month, $this->year, "%Y-%m-%d");
1028          $date = sprintf("%s %02d:%02d:%02d", $day, $this->hour, $this->minute, $this->second);
1029          $newDate = new Date();
1030          $newDate->setDate($date);
1031          return $newDate;
1032      }
1033  
1034      /**
1035       * Get a Date object for the weekday before this one
1036       *
1037       * Get a Date object for the weekday before this one.
1038       * The time of the returned Date object is the same as this time.
1039       *
1040       * @access public
1041       * @return object Date Date representing the previous weekday
1042       */
1043      function getPrevWeekday()
1044      {
1045          $day = Date_Calc::prevWeekday($this->day, $this->month, $this->year, "%Y-%m-%d");
1046          $date = sprintf("%s %02d:%02d:%02d", $day, $this->hour, $this->minute, $this->second);
1047          $newDate = new Date();
1048          $newDate->setDate($date);
1049          return $newDate;
1050      }
1051  
1052  
1053      /**
1054       * Returns the year field of the date object
1055       *
1056       * Returns the year field of the date object
1057       *
1058       * @access public
1059       * @return int the year
1060       */
1061      function getYear()
1062      {
1063          return $this->year;
1064      }
1065  
1066      /**
1067       * Returns the month field of the date object
1068       *
1069       * Returns the month field of the date object
1070       *
1071       * @access public
1072       * @return int the month
1073       */
1074      function getMonth()
1075      {
1076          return $this->month;
1077      }
1078  
1079      /**
1080       * Returns the day field of the date object
1081       *
1082       * Returns the day field of the date object
1083       *
1084       * @access public
1085       * @return int the day
1086       */
1087      function getDay()
1088      {
1089          return (int)$this->day;
1090      }
1091  
1092      /**
1093       * Returns the hour field of the date object
1094       *
1095       * Returns the hour field of the date object
1096       *
1097       * @access public
1098       * @return int the hour
1099       */
1100      function getHour()
1101      {
1102          return $this->hour;
1103      }
1104  
1105      /**
1106       * Returns the minute field of the date object
1107       *
1108       * Returns the minute field of the date object
1109       *
1110       * @access public
1111       * @return int the minute
1112       */
1113      function getMinute()
1114      {
1115          return $this->minute;
1116      }
1117  
1118      /**
1119       * Returns the second field of the date object
1120       *
1121       * Returns the second field of the date object
1122       *
1123       * @access public
1124       * @return int the second
1125       */
1126      function getSecond()
1127      {
1128           return $this->second;
1129      }
1130  
1131      /**
1132       * Set the year field of the date object
1133       *
1134       * Set the year field of the date object, invalid years (not 0-9999) are set to 0.
1135       *
1136       * @access public
1137       * @param int $y the year
1138       */
1139      function setYear($y)
1140      {
1141          if ($y < 0 || $y > 9999) {
1142              $this->year = 0;
1143          } else {
1144              $this->year = $y;
1145          }
1146      }
1147  
1148      /**
1149       * Set the month field of the date object
1150       *
1151       * Set the month field of the date object, invalid months (not 1-12) are set to 1.
1152       *
1153       * @access public
1154       * @param int $m the month
1155       */
1156      function setMonth($m)
1157      {
1158          if ($m < 1 || $m > 12) {
1159              $this->month = 1;
1160          } else {
1161              $this->month = $m;
1162          }
1163      }
1164  
1165      /**
1166       * Set the day field of the date object
1167       *
1168       * Set the day field of the date object, invalid days (not 1-31) are set to 1.
1169       *
1170       * @access public
1171       * @param int $d the day
1172       */
1173      function setDay($d)
1174      {
1175          if ($d > 31 || $d < 1) {
1176              $this->day = 1;
1177          } else {
1178              $this->day = $d;
1179          }
1180      }
1181  
1182      /**
1183       * Set the hour field of the date object
1184       *
1185       * Set the hour field of the date object in 24-hour format.
1186       * Invalid hours (not 0-23) are set to 0.
1187       *
1188       * @access public
1189       * @param int $h the hour
1190       */
1191      function setHour($h)
1192      {
1193          if ($h > 23 || $h < 0) {
1194              $this->hour = 0;
1195          } else {
1196              $this->hour = $h;
1197          }
1198      }
1199  
1200      /**
1201       * Set the minute field of the date object
1202       *
1203       * Set the minute field of the date object, invalid minutes (not 0-59) are set to 0.
1204       *
1205       * @access public
1206       * @param int $m the minute
1207       */
1208      function setMinute($m)
1209      {
1210          if ($m > 59 || $m < 0) {
1211              $this->minute = 0;
1212          } else {
1213              $this->minute = $m;
1214          }
1215      }
1216  
1217      /**
1218       * Set the second field of the date object
1219       *
1220       * Set the second field of the date object, invalid seconds (not 0-59) are set to 0.
1221       *
1222       * @access public
1223       * @param int $s the second
1224       */
1225      function setSecond($s) {
1226          if ($s > 59 || $s < 0) {
1227              $this->second = 0;
1228          } else {
1229              $this->second = $s;
1230          }
1231      }
1232  
1233  } // Date
1234  
1235  
1236  //
1237  // END
1238  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics