[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/ -> Date.php (source)

   1  <?php
   2  
   3  define('HORDE_DATE_SUNDAY',    0);
   4  define('HORDE_DATE_MONDAY',    1);
   5  define('HORDE_DATE_TUESDAY',   2);
   6  define('HORDE_DATE_WEDNESDAY', 3);
   7  define('HORDE_DATE_THURSDAY',  4);
   8  define('HORDE_DATE_FRIDAY',    5);
   9  define('HORDE_DATE_SATURDAY',  6);
  10  
  11  define('HORDE_DATE_MASK_SUNDAY',    1);
  12  define('HORDE_DATE_MASK_MONDAY',    2);
  13  define('HORDE_DATE_MASK_TUESDAY',   4);
  14  define('HORDE_DATE_MASK_WEDNESDAY', 8);
  15  define('HORDE_DATE_MASK_THURSDAY', 16);
  16  define('HORDE_DATE_MASK_FRIDAY',   32);
  17  define('HORDE_DATE_MASK_SATURDAY', 64);
  18  define('HORDE_DATE_MASK_WEEKDAYS', 62);
  19  define('HORDE_DATE_MASK_WEEKEND',  65);
  20  define('HORDE_DATE_MASK_ALLDAYS', 127);
  21  
  22  define('HORDE_DATE_MASK_SECOND',    1);
  23  define('HORDE_DATE_MASK_MINUTE',    2);
  24  define('HORDE_DATE_MASK_HOUR',      4);
  25  define('HORDE_DATE_MASK_DAY',       8);
  26  define('HORDE_DATE_MASK_MONTH',    16);
  27  define('HORDE_DATE_MASK_YEAR',     32);
  28  define('HORDE_DATE_MASK_ALLPARTS', 63);
  29  
  30  /**
  31   * Horde Date wrapper/logic class, including some calculation
  32   * functions.
  33   *
  34   * $Horde: framework/Date/Date.php,v 1.8.10.7 2006/05/04 03:42:32 selsky Exp $
  35   *
  36   * @package Horde_Date
  37   */
  38  class Horde_Date {
  39  
  40      /**
  41       * Year
  42       *
  43       * @var integer
  44       */
  45      var $year;
  46  
  47      /**
  48       * Month
  49       *
  50       * @var integer
  51       */
  52      var $month;
  53  
  54      /**
  55       * Day
  56       *
  57       * @var integer
  58       */
  59      var $mday;
  60  
  61      /**
  62       * Hour
  63       *
  64       * @var integer
  65       */
  66      var $hour = 0;
  67  
  68      /**
  69       * Minute
  70       *
  71       * @var integer
  72       */
  73      var $min = 0;
  74  
  75      /**
  76       * Second
  77       *
  78       * @var integer
  79       */
  80      var $sec = 0;
  81  
  82      /**
  83       * Build a new date object. If $date contains date parts, use them
  84       * to initialize the object.
  85       */
  86      function Horde_Date($date = null)
  87      {
  88          if (is_array($date) || is_object($date)) {
  89              foreach ($date as $key => $val) {
  90                  if (in_array($key, array('year', 'month', 'mday', 'hour', 'min', 'sec'))) {
  91                      $this->$key = (int)$val;
  92                  }
  93              }
  94          } elseif (!is_null($date)) {
  95              // Match YYYY-MM-DD HH:MM:SS and YYYYMMDDHHMMSS.
  96              if (preg_match('/(\d{4})-?(\d{2})-?(\d{2}) ?(\d{2}):?(\d{2}):?(\d{2})/', $date, $parts)) {
  97                  $this->year = (int)$parts[1];
  98                  $this->month = (int)$parts[2];
  99                  $this->mday = (int)$parts[3];
 100                  $this->hour = (int)$parts[4];
 101                  $this->min = (int)$parts[5];
 102                  $this->sec = (int)$parts[6];
 103              } else {
 104                  // Try as a timestamp.
 105                  $parts = @getdate($date);
 106                  if ($parts) {
 107                      $this->year = $parts['year'];
 108                      $this->month = $parts['mon'];
 109                      $this->mday = $parts['mday'];
 110                      $this->hour = $parts['hours'];
 111                      $this->min = $parts['minutes'];
 112                      $this->sec = $parts['seconds'];
 113                  }
 114              }
 115          }
 116      }
 117  
 118      /**
 119       * Return the unix timestamp representation of this date.
 120       *
 121       * @return integer  A unix timestamp.
 122       */
 123      function timestamp()
 124      {
 125          return @mktime($this->hour, $this->min, $this->sec, $this->month, $this->mday, $this->year);
 126      }
 127  
 128      /**
 129       * Return the unix timestamp representation of this date, 12:00am.
 130       *
 131       * @return integer  A unix timestamp.
 132       */
 133      function datestamp()
 134      {
 135          return @mktime(0, 0, 0, $this->month, $this->mday, $this->year);
 136      }
 137  
 138      /**
 139       * @static
 140       */
 141      function isLeapYear($year)
 142      {
 143          if (strlen($year) != 4 || preg_match('/\D/', $year)) {
 144              return false;
 145          }
 146  
 147          return (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0);
 148      }
 149  
 150      /**
 151       * Returns the day of the year (1-366) that corresponds to the
 152       * first day of the given week.
 153       *
 154       * @param integer $week  The week of the year to find the first day of.
 155       * @param integer $year  The year to calculate for.
 156       *
 157       * @return integer  The day of the year of the first day of the given week.
 158       */
 159      function firstDayOfWeek($week, $year)
 160      {
 161          $jan1 = &new Horde_Date(array('year' => $year, 'month' => 1, 'mday' => 1));
 162          $start = $jan1->dayOfWeek();
 163          if ($start > HORDE_DATE_THURSDAY) {
 164              $start -= 7;
 165          }
 166          return (($week * 7) - (7 + $start)) + 1;
 167      }
 168  
 169      /**
 170       * @static
 171       */
 172      function daysInMonth($month, $year)
 173      {
 174          if ($month == 2) {
 175              if (Horde_Date::isLeapYear($year)) {
 176                  return 29;
 177              } else {
 178                  return 28;
 179              }
 180          } elseif ($month == 4 || $month == 6 || $month == 9 || $month == 11) {
 181              return 30;
 182          } else {
 183              return 31;
 184          }
 185      }
 186  
 187      /**
 188       * Return the day of the week (0 = Sunday, 6 = Saturday) of this
 189       * object's date.
 190       *
 191       * @return integer  The day of the week.
 192       */
 193      function dayOfWeek()
 194      {
 195          if ($this->month > 2) {
 196              $month = $this->month - 2;
 197              $year = $this->year;
 198          } else {
 199              $month = $this->month + 10;
 200              $year = $this->year - 1;
 201          }
 202  
 203          $day = (floor((13 * $month - 1) / 5) +
 204                  $this->mday + ($year % 100) +
 205                  floor(($year % 100) / 4) +
 206                  floor(($year / 100) / 4) - 2 *
 207                  floor($year / 100) + 77);
 208  
 209          return (int)($day - 7 * floor($day / 7));
 210      }
 211  
 212      /**
 213       * Returns the day number of the year (0 to 365/366).
 214       *
 215       * @return integer  The day of the year.
 216       */
 217      function dayOfYear()
 218      {
 219          $monthTotals = array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
 220          $dayOfYear = $this->mday + $monthTotals[$this->month - 1];
 221          if (Horde_Date::isLeapYear($this->year) && $this->month > 2) {
 222              $dayOfYear++;
 223          }
 224  
 225          return $dayOfYear;
 226      }
 227  
 228      /**
 229       * Returns week of the year, first Monday is first day of first
 230       * week.
 231       *
 232       * @return integer  The week number.
 233       */
 234      function weekOfYear()
 235      {
 236          return date('W', $this->timestamp());
 237      }
 238  
 239      /**
 240       * Return the number of weeks in the given year (52 or 53).
 241       *
 242       * @param integer $year  The year to count the number of weeks in.
 243       *
 244       * @return integer $numWeeks      The number of weeks in $year.
 245       */
 246      function weeksInYear($year)
 247      {
 248          // Find the last Thursday of the year.
 249          $day = 31;
 250          while (date('w', mktime(0, 0, 0, 12, $day, $year)) != HORDE_DATE_THURSDAY) {
 251              $day--;
 252          }
 253  
 254          $date = &new Horde_Date(array('year' => $year, 'month' => 12, 'mday' => $day));
 255          return $date->weekOfYear();
 256      }
 257  
 258      /**
 259       * Set the date of this object to the $nth weekday of $weekday.
 260       *
 261       * @param integer $weekday  The day of the week (0 = Sunday, etc).
 262       * @param integer $nth      The $nth $weekday to set to (defaults to 1).
 263       */
 264      function setNthWeekday($weekday, $nth = 1)
 265      {
 266          if ($weekday < HORDE_DATE_SUNDAY || $weekday > HORDE_DATE_SATURDAY) {
 267              return false;
 268          }
 269  
 270          $this->mday = 1;
 271          $first = $this->dayOfWeek();
 272          if ($weekday < $first) {
 273              $this->mday = 8 + $weekday - $first;
 274          } else {
 275              $this->mday = $weekday - $first + 1;
 276          }
 277          $this->mday += 7 * $nth - 7;
 278  
 279          $this->correct();
 280  
 281          return true;
 282      }
 283  
 284      function dump($prefix = '')
 285      {
 286          echo ($prefix ? $prefix . ': ' : '') . $this->year . '-' . $this->month . '-' . $this->mday . "<br />\n";
 287      }
 288  
 289      /**
 290       * Is the date currently represented by this object a valid date?
 291       *
 292       * @return boolean  Validity, counting leap years, etc.
 293       */
 294      function isValid()
 295      {
 296          if ($this->year < 0 || $this->year > 9999) {
 297              return false;
 298          }
 299          return checkdate($this->month, $this->mday, $this->year);
 300      }
 301  
 302      /**
 303       * Correct any over- or underflows in any of the date's members.
 304       *
 305       * @param integer $mask  We may not want to correct some overflows.
 306       */
 307      function correct($mask = HORDE_DATE_MASK_ALLPARTS)
 308      {
 309          if ($mask & HORDE_DATE_MASK_SECOND) {
 310              while ($this->sec < 0) {
 311                  $this->min--;
 312                  $this->sec += 60;
 313              }
 314              while ($this->sec > 59) {
 315                  $this->min++;
 316                  $this->sec -= 60;
 317              }
 318          }
 319  
 320          if ($mask & HORDE_DATE_MASK_MINUTE) {
 321              while ($this->min < 0) {
 322                  $this->hour--;
 323                  $this->min += 60;
 324              }
 325              while ($this->min > 59) {
 326                  $this->hour++;
 327                  $this->min -= 60;
 328              }
 329          }
 330  
 331          if ($mask & HORDE_DATE_MASK_HOUR) {
 332              while ($this->hour < 0) {
 333                  $this->mday--;
 334                  $this->hour += 24;
 335              }
 336              while ($this->hour > 23) {
 337                  $this->mday++;
 338                  $this->hour -= 24;
 339              }
 340          }
 341  
 342          if ($mask & HORDE_DATE_MASK_MONTH) {
 343              while ($this->month > 12) {
 344                  $this->year++;
 345                  $this->month -= 12;
 346              }
 347              while ($this->month < 1) {
 348                  $this->year--;
 349                  $this->month += 12;
 350              }
 351          }
 352  
 353          if ($mask & HORDE_DATE_MASK_DAY) {
 354              while ($this->mday > Horde_Date::daysInMonth($this->month, $this->year)) {
 355                  $this->mday -= Horde_Date::daysInMonth($this->month, $this->year);
 356                  $this->month++;
 357                  $this->correct(HORDE_DATE_MASK_MONTH);
 358              }
 359              while ($this->mday < 1) {
 360                  $this->month--;
 361                  $this->correct(HORDE_DATE_MASK_MONTH);
 362                  $this->mday += Horde_Date::daysInMonth($this->month, $this->year);
 363              }
 364          }
 365      }
 366  
 367      /**
 368       * Compare this date to another date object to see which one is
 369       * greater (later). Assumes that the dates are in the same
 370       * timezone.
 371       *
 372       * @param mixed $date  The date to compare to.
 373       *
 374       * @return integer  ==  0 if the dates are equal
 375       *                  >=  1 if this date is greater (later)
 376       *                  <= -1 if the other date is greater (later)
 377       */
 378      function compareDate($date)
 379      {
 380          if (!is_a($date, 'Horde_Date')) {
 381              $date = &new Horde_Date($date);
 382          }
 383  
 384          if ($this->year != $date->year) {
 385              return $this->year - $date->year;
 386          } elseif ($this->month != $date->month) {
 387              return $this->month - $date->month;
 388          } else {
 389              return $this->mday - $date->mday;
 390          }
 391      }
 392  
 393      /**
 394       * Compare this to another date object by time, to see which one
 395       * is greater (later). Assumes that the dates are in the same
 396       * timezone.
 397       *
 398       * @param mixed $date  The date to compare to.
 399       *
 400       * @return integer  ==  0 if the dates are equal
 401       *                  >=  1 if this date is greater (later)
 402       *                  <= -1 if the other date is greater (later)
 403       */
 404      function compareTime($date)
 405      {
 406          if (!is_a($date, 'Horde_Date')) {
 407              $date = &new Horde_Date($date);
 408          }
 409  
 410          if ($this->hour != $date->hour) {
 411              return $this->hour - $date->hour;
 412          } elseif ($this->min != $date->min) {
 413              return $this->min - $date->min;
 414          } else {
 415              return $this->sec - $date->sec;
 416          }
 417      }
 418  
 419      /**
 420       * Compare this to another date object, including times, to see
 421       * which one is greater (later). Assumes that the dates are in the
 422       * same timezone.
 423       *
 424       * @param mixed $date  The date to compare to.
 425       *
 426       * @return integer  ==  0 if the dates are equal
 427       *                  >=  1 if this date is greater (later)
 428       *                  <= -1 if the other date is greater (later)
 429       */
 430      function compareDateTime($date)
 431      {
 432          if (!is_a($date, 'Horde_Date')) {
 433              $date = &new Horde_Date($date);
 434          }
 435  
 436          if ($diff = $this->compareDate($date)) {
 437              return $diff;
 438          } else {
 439              return $this->compareTime($date);
 440          }
 441      }
 442  
 443      /**
 444       * Get the time offset for local time zone.
 445       *
 446       * @param boolean $colon  Place a colon between hours and minutes?
 447       *
 448       * @return string  Timezone offset as a string in the format +HH:MM.
 449       */
 450      function tzOffset($colon = true)
 451      {
 452          $secs = date('Z', $this->timestamp());
 453  
 454          if ($secs < 0) {
 455              $sign = '-';
 456              $secs = -$secs;
 457          } else {
 458              $sign = '+';
 459          }
 460          $colon = $colon ? ':' : '';
 461          $mins = intval(($secs + 30) / 60);
 462          return sprintf('%s%02d%s%02d',
 463                         $sign, $mins / 60, $colon, $mins % 60);
 464      }
 465  
 466      /**
 467       * Format time in ISO-8601 format.
 468       *
 469       * @return string  Date and time in ISO-8601 format.
 470       */
 471      function iso8601DateTime()
 472      {
 473          $tzoff = $this->tzOffset();
 474          $date = $this->year . '-' . $this->month . '-' . $this->mday;
 475          $time = $this->hour . ':' . $this->min . ':' . $this->sec;
 476  
 477          return $date . 'T' . $time . $tzoff;
 478      }
 479  
 480      /**
 481       * Format time in RFC 2822 format.
 482       *
 483       * @return string  Date and time in RFC 2822 format.
 484       */
 485      function rfc2822DateTime()
 486      {
 487          return date('D, j M Y H:i:s ', $this->timestamp()) . $this->tzOffset(false);
 488      }
 489  
 490      /**
 491       * Format time in RFC 3339 format.
 492       *
 493       * @return string  Date and time in RFC 3339 format.
 494       */
 495      function rfc3339DateTime()
 496      {
 497          return date('Y-m-d\TH:i', $this->timestamp());
 498      }
 499  
 500      /**
 501       * Format time to standard 'ctime' format.
 502       *
 503       * @return string  Date and time.
 504       */
 505      function cTime($time = false)
 506      {
 507          return date('D M j H:i:s Y', $this->timestamp());
 508      }
 509  
 510      /**
 511       * Format date and time using strftime() format.
 512       *
 513       * @return string  strftime() formatted date and time.
 514       */
 515      function strftime($format)
 516      {
 517          return strftime($format, $this->timestamp());
 518      }
 519  
 520  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7