[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/date/date_php4/ -> date_php4_calc.inc (source)

   1  <?php
   2  /**
   3   * Calculates and manipulates dates with no reliance on 32-bit system
   4   * timestamps, to work on dates before 1970 and after 2038.
   5   *
   6   * This file must be included whenever these functions are used, it is
   7   * not automatically available.
   8   *
   9   * The functions below were derived from code obtained from
  10   * http://pear.php.net/package/Date/Calc.php, licensed as follows:
  11   *
  12   * Copyright (c) 1999-2006 Monte Ohrt, Pierre-Alain Joye, Daniel Convissor
  13   * All rights reserved.
  14   *
  15   * Redistribution and use in source and binary forms, with or without
  16   * modification, are permitted under the terms of the BSD License.
  17   *
  18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21   * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22   * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24   * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  28   * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29   * POSSIBILITY OF SUCH DAMAGE.
  30   */
  31  define('DATE_CALC_FORMAT', '%Y-%m-%d');
  32  
  33  // Some functions from the low level library are needed here, make sure
  34  // they are available.
  35  require_once('./'. drupal_get_path('module', 'date_php4') .'/date_php4_lib.inc');
  36  
  37  /**
  38   * The default value for each method's $format parameter
  39   *
  40   * The default is '%Y%m%d'.  To override this default, define
  41   * this constant before including Calc.php.
  42   *
  43   * @since Constant available since Release 1.4.4
  44   */
  45  /**
  46   * Formats the date in the given format, much like strfmt()
  47   *
  48   * This function is used to alleviate the problem with 32-bit numbers for
  49   * dates pre 1970 or post 2038, as strfmt() has on most systems.
  50   * Most of the formatting options are compatible.
  51   *
  52   * Formatting options:
  53   * <pre>
  54   * %a   abbreviated weekday name (Sun, Mon, Tue)
  55   * %A   full weekday name (Sunday, Monday, Tuesday)
  56   * %b   abbreviated month name (Jan, Feb, Mar)
  57   * %B   full month name (January, February, March)
  58   * %d   day of month (range 00 to 31)
  59   * %e   day of month, single digit (range 0 to 31)
  60   * %E   number of days since unspecified epoch (integer)
  61   *    (%E is useful for passing a date in a URL as
  62   *    an integer value. Then simply use
  63   *    date_calc_days_to_date() to convert back to a date.)
  64   * %j   day of year (range 001 to 366)
  65   * %m   month as decimal number (range 1 to 12)
  66   * %n   newline character (\n)
  67   * %t   tab character (\t)
  68   * %w   weekday as decimal (0 = Sunday)
  69   * %U   week number of current year, first sunday as first week
  70   * %y   year as decimal (range 00 to 99)
  71   * %Y   year as decimal including century (range 0000 to 9999)
  72   * %%   literal '%'
  73   * </pre>
  74   *
  75   * @param int  $day
  76   *   The day of the month.
  77   * @param int  $month
  78   *   The month.
  79   * @param int  $year
  80   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
  81   * @param string $format
  82   *   The format string.
  83   *
  84   * @return string
  85   *   The date in the desired format.
  86   */
  87  function date_calc_format($day, $month, $year, $format = DATE_CALC_FORMAT) {
  88    if (!date_calc_is_valid($day, $month, $year)) {
  89      $year  = date_calc_date_now('%Y');
  90      $month = date_calc_date_now('%m');
  91      $day   = date_calc_date_now('%d');
  92    }
  93    $output = '';
  94    for ($strpos = 0; $strpos < strlen($format); $strpos++) {
  95      $char = substr($format, $strpos, 1);
  96      if ($char == '%') {
  97        $nextchar = substr($format, $strpos + 1, 1);
  98        switch ($nextchar) {
  99          case 'a':
 100            $output .= date_calc_get_weekday_abbrname($day, $month, $year);
 101            break;
 102          case 'A':
 103            $output .= date_calc_get_weekday_fullname($day, $month, $year);
 104            break;
 105          case 'b':
 106            $output .= date_calc_get_month_abbrname($month);
 107            break;
 108          case 'B':
 109            $output .= date_calc_get_month_fullname($month);
 110            break;
 111          case 'd':
 112            $output .= date_pad($day);
 113            break;
 114          case 'e':
 115            $output .= $day;
 116            break;
 117          case 'E':
 118            $output .= date_calc_date_to_days($day, $month, $year);
 119            break;
 120          case 'j':
 121            $output .= date_calc_julian_date($day, $month, $year);
 122            break;
 123          case 'm':
 124            $output .= date_pad($month);
 125            break;
 126          case 'n':
 127            $output .= "\n";
 128            break;
 129          case 't':
 130            $output .= "\t";
 131            break;
 132          case 'w':
 133            $output .= date_dow($day, $month, $year);
 134            break;
 135          case 'U':
 136            $output .= date_calc_week_of_year($day, $month, $year);
 137            break;
 138          case 'y':
 139            $output .= substr(date_pad($year, 4), 2, 2);
 140            break;
 141          case 'Y':
 142            $output .= date_pad($year, 4);
 143            break;
 144          case '%':
 145            $output .= '%';
 146            break;
 147          default:
 148            $output .= $char . $nextchar;
 149        }
 150        $strpos++;
 151      }
 152      else {
 153        $output .= $char;
 154      }
 155    }
 156    return $output;
 157  }
 158  
 159  /**
 160   * Returns true for valid date, false for invalid date.
 161   * Renamed this function because we already have a similar function that
 162   * expects a full date as a parameter.
 163   *
 164   * @param int  $day
 165   *   the day of the month
 166   * @param int  $month
 167   *   the month
 168   * @param int  $year
 169   *   the year, using 2005, not 05. Do not add leading 0's for years prior to 1000.
 170   *
 171   * @return boolean
 172   */
 173  function date_calc_is_valid($day, $month, $year) {
 174    if ($year > variable_get('date_max_year', 4000) || $year < variable_get('date_min_year', 1)) {
 175      return false;
 176    }
 177    if (!checkdate($month, $day, $year)) {
 178      // Checkdate won't work on very old dates in PHP 4, need more testing.
 179      if (!date_gmmktime(0, 0, 0, $month, $day, $year)) {
 180        return false;
 181      }
 182    }
 183    return true;
 184  }
 185  
 186  /**
 187   * Converts a date to number of days since a distant unspecified epoch
 188   *
 189   * @param int  $day
 190   *   The day of the month.
 191   * @param int  $month
 192   *   The month.
 193   * @param int  $year
 194   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
 195   *
 196   * @return integer
 197   *   The number of days since the date_calc epoch.
 198   */
 199  function date_calc_date_to_days($day, $month, $year) {
 200    $century = (int)substr($year, 0, 2);
 201    $year = (int)substr($year, 2, 2);
 202    if ($month > 2) {
 203      $month -= 3;
 204    }
 205    else {
 206      $month += 9;
 207      if ($year) {
 208        $year--;
 209      }
 210      else {
 211        $year = 99;
 212        $century --;
 213      }
 214    }
 215    return (floor((146097 * $century) / 4 ) +
 216      floor((1461 * $year) / 4 ) +
 217      floor((153 * $month + 2) / 5 ) +
 218      $day + 1721119);
 219  }
 220  
 221  /**
 222   * Converts number of days to a distant unspecified epoch
 223   *
 224   * @param int  $days
 225   *   The number of days since the date_calc epoch.
 226   * @param string $format
 227   *   The string indicating how to format the output.
 228   *
 229   * @return string
 230   *   The date in the desired format.
 231   */
 232  function date_calc_days_to_date($days, $format = DATE_CALC_FORMAT) {
 233    $days   -= 1721119;
 234    $century = floor((4 * $days - 1) / 146097);
 235    $days  = floor(4 * $days - 1 - 146097 * $century);
 236    $day   = floor($days / 4);
 237  
 238    $year  = floor((4 * $day +  3) / 1461);
 239    $day   = floor(4 * $day +  3 - 1461 * $year);
 240    $day   = floor(($day +  4) / 4);
 241  
 242    $month   = floor((5 * $day - 3) / 153);
 243    $day   = floor(5 * $day - 3 - 153 * $month);
 244    $day   = floor(($day +  5) /  5);
 245  
 246    if ($month < 10) {
 247      $month +=3;
 248    }
 249    else {
 250      $month -=9;
 251      if ($year++ == 99) {
 252        $year = 0;
 253        $century++;
 254      }
 255    }
 256    $century = date_pad($century);
 257    $year  = date_pad($year);
 258    return date_calc_format($day, $month, $century . $year, $format);
 259  }
 260  
 261  /**
 262   * Converts from Gregorian Year-Month-Day to ISO Year-WeekNumber-WeekDay
 263   *
 264   * Uses ISO 8601 definitions.  Algorithm by Rick McCarty, 1999 at
 265   * http://personal.ecu.edu/mccartyr/ISOwdALG.txt .
 266   * Transcribed to PHP by Jesus M. Castagnetto.
 267   *
 268   * @param int  $day
 269   *   The day of the month.
 270   * @param int  $month
 271   *   The month.
 272   * @param int  $year
 273   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
 274   *
 275   * @return string
 276   *   The date in ISO Year-WeekNumber-WeekDay format.
 277   */
 278  function date_calc_gregorian_to_ISO($day, $month, $year) {
 279    $mnth = array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
 280    $y_isleap = date_is_leap_year($year);
 281    $y_1_isleap = date_is_leap_year($year - 1);
 282    $day_of_year_number = $day + $mnth[$month - 1];
 283    if ($y_isleap && $month > 2) {
 284      $day_of_year_number++;
 285    }
 286    // find Jan 1 weekday (monday = 1, sunday = 7)
 287    $yy = ($year - 1) % 100;
 288    $c = ($year - 1) - $yy;
 289    $g = $yy + intval($yy / 4);
 290    $jan1_weekday = 1 + intval((((($c / 100) % 4) * 5) + $g) % 7);
 291    // weekday for year-month-day
 292    $h = $day_of_year_number + ($jan1_weekday - 1);
 293    $weekday = 1 + intval(($h - 1) % 7);
 294    // find if Y M D falls in YearNumber Y-1, WeekNumber 52 or
 295    if ($day_of_year_number <= (8 - $jan1_weekday) && $jan1_weekday > 4) {
 296      $yearnumber = $year - 1;
 297      if ($jan1_weekday == 5 || ($jan1_weekday == 6 && $y_1_isleap)) {
 298        $weeknumber = 53;
 299      }
 300      else {
 301        $weeknumber = 52;
 302      }
 303    }
 304    else {
 305      $yearnumber = $year;
 306    }
 307    // find if Y M D falls in YearNumber Y+1, WeekNumber 1
 308    if ($yearnumber == $year) {
 309      if ($y_isleap) {
 310        $i = 366;
 311      }
 312      else {
 313        $i = 365;
 314      }
 315      if (($i - $day_of_year_number) < (4 - $weekday)) {
 316        $yearnumber++;
 317        $weeknumber = 1;
 318      }
 319    }
 320    // find if Y M D falls in YearNumber Y, WeekNumber 1 through 53
 321    if ($yearnumber == $year) {
 322      $j = $day_of_year_number + (7 - $weekday) + ($jan1_weekday - 1);
 323      $weeknumber = intval($j / 7);
 324      if ($jan1_weekday > 4) {
 325        $weeknumber--;
 326      }
 327    }
 328    // put it all together
 329    if ($weeknumber < 10) {
 330      $weeknumber = '0'. $weeknumber;
 331    }
 332    return $yearnumber .'-'. $weeknumber .'-'. $weekday;
 333  }
 334  
 335  /**
 336   * Determines julian date of the given season
 337   *
 338   * Adapted from previous work in Java by James Mark Hamilton.
 339   *
 340   * @param string $season
 341   *   The season to get the date for:
 342   *   VERNALEQUINOX, SUMMERSOLSTICE, AUTUMNALEQUINOX, or WINTERSOLSTICE.
 343   * @param string $year
 344   *   The year in four digit format.  Must be between -1000BC and 3000AD.
 345   *
 346   * @return float
 347   *   The julian date the season starts on.
 348   *
 349   * @author James Mark Hamilton <mhamilton@qwest.net>
 350   * @author Robert Butler <rob@maxwellcreek.org>
 351   */
 352  function date_calc_date_season($season, $year = 0) {
 353    if ($year == '') {
 354      $year = date_calc_get_year();
 355    }
 356    if (($year >= -1000) && ($year <= 1000)) {
 357      $y = $year / 1000.0;
 358      switch ($season) {
 359        case 'VERNALEQUINOX':
 360          $date_calc_julian_date = (((((((-0.00071 * $y) - 0.00111) * $y) + 0.06134) * $y) + 365242.1374) * $y) + 1721139.29189;
 361          break;
 362        case 'SUMMERSOLSTICE':
 363          $date_calc_julian_date = (((((((0.00025 * $y) + 0.00907) * $y) - 0.05323) * $y) + 365241.72562) * $y) + 1721233.25401;
 364          break;
 365        case 'AUTUMNALEQUINOX':
 366          $date_calc_julian_date = (((((((0.00074 * $y) - 0.00297) * $y) - 0.11677) * $y) + 365242.49558) * $y) + 1721325.70455;
 367          break;
 368        case 'WINTERSOLSTICE':
 369        default:
 370          $date_calc_julian_date = (((((((-0.00006 * $y) - 0.00933) * $y) - 0.00769) * $y) + 365242.88257) * $y) + 1721414.39987;
 371      }
 372    }
 373    elseif (($year > 1000) && ($year <= 3000)) {
 374      $y = ($year - 2000) / 1000;
 375      switch ($season) {
 376        case 'VERNALEQUINOX':
 377          $date_calc_julian_date = (((((((-0.00057 * $y) - 0.00411) * $y) + 0.05169) * $y) + 365242.37404) * $y) + 2451623.80984;
 378          break;
 379        case 'SUMMERSOLSTICE':
 380          $date_calc_julian_date = (((((((-0.0003 * $y) + 0.00888) * $y) + 0.00325) * $y) + 365241.62603) * $y) + 2451716.56767;
 381          break;
 382        case 'AUTUMNALEQUINOX':
 383          $date_calc_julian_date = (((((((0.00078 * $y) + 0.00337) * $y) - 0.11575) * $y) + 365242.01767) * $y) + 2451810.21715;
 384          break;
 385        case 'WINTERSOLSTICE':
 386        default:
 387          $date_calc_julian_date = (((((((0.00032 * $y) - 0.00823) * $y) - 0.06223) * $y) + 365242.74049) * $y) + 2451900.05952;
 388      }
 389    }
 390    return $date_calc_julian_date;
 391  }
 392  
 393  /**
 394   * Returns the current local date.
 395   *
 396   * NOTE: This function retrieves the local date using strftime(),
 397   * which may or may not be 32-bit safe on your system.
 398   *
 399   * @param string $format
 400   *   The string indicating how to format the output.
 401   *
 402   * @return string
 403   *   The current date in the specified format.
 404   */
 405  function date_calc_date_now($format = DATE_CALC_FORMAT) {
 406    return strftime($format, time());
 407  }
 408  
 409  /**
 410   * Returns the current local year in format CCYY.
 411   *
 412   * @param  int $days
 413   *   An integer calculated by date_calc_date_to_days().
 414   *
 415   * @return string
 416   *   The current year in four digit format.
 417   */
 418  function date_calc_get_year() {
 419    return date_calc_date_now('%Y');
 420  }
 421  
 422  /**
 423   * Returns the current local month in format MM
 424   *
 425   * @param  int $days
 426   *   An integer calculated by date_calc_date_to_days().
 427   *
 428   * @return string
 429   *   The current month in two digit format.
 430   */
 431  function date_calc_get_month() {
 432    return date_calc_date_now('%m');
 433  }
 434  
 435  /**
 436   * Returns the current local day in format DD
 437   *
 438   * @param  int $days
 439   *   An integer calculated by date_calc_date_to_days().
 440   *
 441   * @return string
 442   *   The current day of the month in two digit format.
 443   */
 444  function date_calc_get_day() {
 445    return date_calc_date_now('%d');
 446  }
 447  
 448  /**
 449   * Returns number of days since 31 December of year before given date.
 450   *
 451   * @param int  $day
 452   *   The day of the month.
 453   * @param int  $month
 454   *   The month.
 455   * @param int  $year
 456   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
 457   *
 458   * @return int
 459   *   The julian date for the date.
 460   */
 461  function date_calc_julian_date($day = 0, $month = 0, $year = 0) {
 462    if (empty($year)) {
 463      $year = date_calc_get_year();
 464    }
 465    if (empty($month)) {
 466      $month = date_calc_get_month();
 467    }
 468    if (empty($day)) {
 469      $day = date_calc_get_day();
 470    }
 471    $days = array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
 472    $julian = ($days[$month - 1] + $day);
 473    if ($month > 2 && date_is_leap_year($year)) {
 474      $julian++;
 475    }
 476    return $julian;
 477  }
 478  
 479  /**
 480   * Returns the full weekday name for the given date
 481   *
 482   * @param int  $day
 483   *   The day of the month.
 484   * @param int  $month
 485   *   The month.
 486   * @param int  $year
 487   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
 488   *
 489   * @return string
 490   *   The full name of the day of the week/
 491   */
 492  function date_calc_get_weekday_fullname($day = 0, $month = 0, $year = 0) {
 493    if (empty($year)) {
 494      $year = date_calc_get_year();
 495    }
 496    if (empty($month)) {
 497      $month = date_calc_get_month();
 498    }
 499    if (empty($day)) {
 500      $day = date_calc_get_day();
 501    }
 502    $weekday_names = date_week_days();
 503    $weekday = date_dow($day, $month, $year);
 504    return $weekday_names[$weekday];
 505  }
 506  
 507  /**
 508   * Returns the abbreviated weekday name for the given date.
 509   *
 510   * @param int  $day
 511   *   The day of the month.
 512   * @param int  $month
 513   *   The month.
 514   * @param int  $year
 515   *   The year.  Use the complete year instead of the abbreviated version.
 516   *   E.g. use 2005, not 05. Do not add leading 0's for years prior to 1000.
 517   * @param int  $length
 518   *   The length of abbreviation.
 519   *
 520   * @return string
 521   *   The abbreviated name of the day of the week.
 522   */
 523  function date_calc_get_weekday_abbrname($day = 0, $month = 0, $year = 0) {
 524    if (empty($year)) {
 525      $year = date_calc_get_year();
 526    }
 527    if (empty($month)) {
 528      $month = date_calc_get_month();
 529    }
 530    if (empty($day)) {
 531      $day = date_calc_get_day();
 532    }
 533    $weekday_names = date_week_days_abbr();
 534    $weekday = date_dow($day, $month, $year);
 535    return $weekday_names[$weekday];
 536  }
 537  
 538  /**
 539   * Returns the full month name for the given month.
 540   *
 541   * @param int  $month
 542   *   The month.
 543   *
 544   * @return string
 545   *   The full name of the month.
 546   */
 547  function date_calc_get_month_fullname($month) {
 548    $month = (int)$month;
 549    if (empty($month)) {
 550      $month = (int)date_calc_get_month();
 551    }
 552    $month_names = date_month_names();
 553    return $month_names[$month];
 554  }
 555  
 556  /**
 557   * Returns the abbreviated month name for the given month.
 558   *
 559   * @param int  $month
 560   *   The month.
 561   * @param int  $length
 562   *   The length of abbreviation.
 563   *
 564   * @return string
 565   *   The abbreviated name of the month.
 566   */
 567  function date_calc_get_month_abbrname($month, $length = 3) {
 568    $month = (int)$month;
 569    if (empty($month)) {
 570      $month = date_calc_get_month();
 571    }
 572    return drupal_substr(date_calc_get_month_fullname($month), 0, $length);
 573  }
 574  
 575  /**
 576   * Returns the numeric month from the month name or an abreviation
 577   * Both August and Aug would return 8.
 578   *
 579   * @param string $month
 580   *   The name of the month to examine. Case insensitive.
 581   *
 582   * @return integer
 583   *   The month's number.
 584   */
 585  function date_calc_get_month_from_fullname($month) {
 586    $month = strtolower($month);
 587    $months = date_month_names();
 588    while (list($id, $name) = each($months)) {
 589      if (ereg($month, strtolower($name))) {
 590        return $id;
 591      }
 592    }
 593    return 0;
 594  }
 595  
 596  /**
 597   * Returns week of the year, first Sunday is first day of first week.
 598   *
 599   * @param int  $day
 600   *   The day of the month.
 601   * @param int  $month
 602   *   The month.
 603   * @param int  $year
 604   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
 605   *
 606   * @return int
 607   *   The number of the week in the year.
 608   */
 609  function date_calc_week_of_year($day = 0, $month = 0, $year = 0) {
 610    if (empty($year)) {
 611      $year = date_calc_get_year();
 612    }
 613    if (empty($month)) {
 614      $month = date_calc_get_month();
 615    }
 616    if (empty($day)) {
 617      $day = date_calc_get_day();
 618    }
 619    $iso  = date_calc_gregorian_to_ISO($day, $month, $year);
 620    $parts  = explode('-', $iso);
 621    $week_number = intval($parts[1]);
 622    return $week_number;
 623  }
 624  
 625  /**
 626   * Returns quarter of the year for given date.
 627   *
 628    * @param int  $day
 629   *   The day of the month.
 630   * @param int  $month
 631   *   The month.
 632   * @param int  $year
 633   *
 634   * @return int
 635   *   The number of the quarter in the year.
 636   */
 637  function date_calc_quarter_of_year($day = 0, $month = 0, $year = 0) {
 638    if (empty($year)) {
 639      $year = date_calc_get_year();
 640    }
 641    if (empty($month)) {
 642      $month = date_calc_get_month();
 643    }
 644    if (empty($day)) {
 645      $day = date_calc_get_day();
 646    }
 647    $year_quarter = intval(($month - 1) / 3 + 1);
 648    return $year_quarter;
 649  }
 650  
 651  /**
 652   * Find the number of days in the given month.
 653   *
 654   * @param int  $month
 655   *   The month.
 656   * @param int  $year
 657   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
 658   *
 659   * @return int
 660   *   The number of days the month has.
 661   */
 662  function date_calc_days_in_month($month = 0, $year = 0) {
 663    if (empty($year)) {
 664      $year = date_calc_get_year();
 665    }
 666    if (empty($month)) {
 667      $month = date_calc_get_month();
 668    }
 669    if ($year == 1582 && $month == 10) {
 670      return 21;  // October 1582 only had 1st-4th and 15th-31st
 671    }
 672    if ($month == 2) {
 673      if (date_is_leap_year($year)) {
 674        return 29;
 675       }
 676       else {
 677        return 28;
 678      }
 679    }
 680    elseif ($month == 4 or $month == 6 or $month == 9 or $month == 11) {
 681      return 30;
 682    }
 683    else {
 684      return 31;
 685    }
 686  }
 687  
 688  /**
 689   * Returns the number of rows on a calendar month. Useful for determining
 690   * the number of rows when displaying a typical month calendar.
 691   *
 692   * @param int  $month
 693   *   The month.
 694   * @param int  $year
 695   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
 696   *
 697   * @return int
 698   *   The number of weeks the month has.
 699   */
 700  function date_calc_weeks_in_month($month = 0, $year = 0) {
 701    if (empty($year)) {
 702      $year = date_calc_get_year();
 703    }
 704    if (empty($month)) {
 705      $month = date_calc_get_month();
 706    }
 707    $FDOM = date_calc_first_of_month_weekday($month, $year);
 708    if (variable_get('date_first_day', 1)==1 && $FDOM==0) {
 709      $first_week_days = 7 - $FDOM + variable_get('date_first_day', 1);
 710      $weeks = 1;
 711    }
 712    elseif (variable_get('date_first_day', 1)==0 && $FDOM == 6) {
 713      $first_week_days = 7 - $FDOM + variable_get('date_first_day', 1);
 714      $weeks = 1;
 715    }
 716    else {
 717      $first_week_days = variable_get('date_first_day', 1) - $FDOM;
 718      $weeks = 0;
 719    }
 720    $first_week_days %= 7;
 721    return ceil((date_calc_days_in_month($month, $year)
 722      - $first_week_days) / 7) + $weeks;
 723  }
 724  
 725  /**
 726   * Return an array with days in week.
 727   *
 728   * @param int  $day
 729   *   The day of the month.
 730   * @param int  $month
 731   *   The month.
 732   * @param int  $year
 733   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
 734   * @param string $format
 735   *   The string indicating how to format the output.
 736   *
 737   * @return array
 738   *   $week[$weekday].
 739   */
 740  function date_calc_get_calendar_week($day = 0, $month = 0, $year = 0, $format = DATE_CALC_FORMAT) {
 741    if (empty($year)) {
 742      $year = date_calc_get_year();
 743    }
 744    if (empty($month)) {
 745      $month = date_calc_get_month();
 746    }
 747    if (empty($day)) {
 748      $day = date_calc_get_day();
 749    }
 750    $week_array = array();
 751    // date for the column of week
 752    $curr_day = date_calc_begin_of_week($day, $month, $year, '%E');
 753    for ($counter = 0; $counter <= 6; $counter++) {
 754      $week_array[$counter] = date_calc_days_to_date($curr_day, $format);
 755      $curr_day++;
 756    }
 757    return $week_array;
 758  }
 759  
 760  /**
 761   * Return a set of arrays to construct a calendar month for the given date.
 762   *
 763   * @param int  $month
 764   *   The month.
 765   * @param int  $year
 766   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
 767   * @param string $format
 768   *   The string indicating how to format the output.
 769   *
 770   * @return array
 771   *   $month[$row][$col].
 772   */
 773  function date_calc_get_calendar_month($month = 0, $year = 0, $format = DATE_CALC_FORMAT) {
 774    if (empty($year)) {
 775      $year = date_calc_get_year();
 776    }
 777    if (empty($month)) {
 778      $month = date_calc_get_month();
 779    }
 780    $month_array = array();
 781    // date for the first row, first column of calendar month
 782    if (variable_get('date_first_day', 1) == 1) {
 783      if (date_calc_first_of_month_weekday($month, $year) == 0) {
 784        $curr_day = date_calc_date_to_days('01', $month, $year) - 6;
 785      }
 786      else {
 787        $curr_day = date_calc_date_to_days('01', $month, $year)
 788          - date_calc_first_of_month_weekday($month, $year) + 1;
 789      }
 790    }
 791    else {
 792      $curr_day = (date_calc_date_to_days('01', $month, $year)
 793        - date_calc_first_of_month_weekday($month, $year));
 794    }
 795    // number of days in this month
 796    $date_calc_days_in_month = date_calc_days_in_month($month, $year);
 797    $date_calc_weeks_in_month = date_calc_weeks_in_month($month, $year);
 798    for ($row_counter = 0; $row_counter < $date_calc_weeks_in_month; $row_counter++) {
 799      for ($column_counter = 0; $column_counter <= 6; $column_counter++) {
 800        $month_array[$row_counter][$column_counter] =
 801          date_calc_days_to_date($curr_day , $format);
 802        $curr_day++;
 803      }
 804    }
 805    return $month_array;
 806  }
 807  
 808  /**
 809   * Return a set of arrays to construct a calendar year for the given date
 810   *
 811   * @param int  $year
 812   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
 813   * @param string $format
 814   *   The string indicating how to format the output.
 815   *
 816   * @return array $year[$month][$row][$col]
 817   */
 818  function date_calc_get_calendar_year($year = 0, $format = DATE_CALC_FORMAT) {
 819    if (empty($year)) {
 820      $year = date_calc_get_year();
 821    }
 822    $year_array = array();
 823    for ($curr_month = 0; $curr_month <= 11; $curr_month++) {
 824      $year_array[$curr_month] =
 825        date_calc_get_calendar_month($curr_month + 1,
 826          $year, $format);
 827    }
 828    return $year_array;
 829  }
 830  
 831  /**
 832   * Returns date of day before given date.
 833   *
 834   * @param int  $day
 835   *   The day of the month.
 836   * @param int  $month
 837   *   The month.
 838   * @param int  $year
 839   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
 840   * @param string $format
 841   *   The string indicating how to format the output.
 842   *
 843   * @return string
 844   *   The date in the desired format.
 845   */
 846  function date_calc_prev_day($day = 0, $month = 0, $year = 0, $format = DATE_CALC_FORMAT) {
 847    if (empty($year)) {
 848      $year = date_calc_get_year();
 849    }
 850    if (empty($month)) {
 851      $month = date_calc_get_month();
 852    }
 853    if (empty($day)) {
 854      $day = date_calc_get_day();
 855    }
 856    $days = date_calc_date_to_days($day, $month, $year);
 857    return date_calc_days_to_date($days - 1, $format);
 858  }
 859  
 860  /**
 861   * Returns date of day after given date
 862   *
 863   * @param int  $day
 864   *   The day of the month.
 865   * @param int  $month
 866   *   The month.
 867   * @param int  $year
 868   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
 869   * @param string $format
 870   *   The string indicating how to format the output.
 871   *
 872   * @return string
 873   *   The date in the desired format.
 874   */
 875  function date_calc_next_day($day = 0, $month = 0, $year = 0, $format = DATE_CALC_FORMAT) {
 876    if (empty($year)) {
 877      $year = date_calc_get_year();
 878    }
 879    if (empty($month)) {
 880      $month = date_calc_get_month();
 881    }
 882    if (empty($day)) {
 883      $day = date_calc_get_day();
 884    }
 885    $days = date_calc_date_to_days($day, $month, $year);
 886    return date_calc_days_to_date($days + 1, $format);
 887  }
 888  
 889  /**
 890   * Returns date of the previous weekday, skipping from Monday to Friday
 891   *
 892   * @param int  $day
 893   *   The day of the month.
 894   * @param int  $month
 895   *   The month.
 896   * @param int  $year
 897   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
 898   * @param string $format
 899   *   The string indicating how to format the output.
 900   *
 901   * @return string
 902   *   The date in the desired format.
 903   */
 904  function date_calc_prev_weekday($day = 0, $month = 0, $year = 0, $format = DATE_CALC_FORMAT) {
 905    if (empty($year)) {
 906      $year = date_calc_get_year();
 907    }
 908    if (empty($month)) {
 909      $month = date_calc_get_month();
 910    }
 911    if (empty($day)) {
 912      $day = date_calc_get_day();
 913    }
 914    $days = date_calc_date_to_days($day, $month, $year);
 915    if (date_dow($day, $month, $year) == 1) {
 916      $days -= 3;
 917    }
 918    elseif (date_dow($day, $month, $year) == 0) {
 919      $days -= 2;
 920    }
 921    else {
 922      $days -= 1;
 923    }
 924    return date_calc_days_to_date($days, $format);
 925  }
 926  
 927  /**
 928   * Returns date of the next weekday of given date, skipping from
 929   * Friday to Monday.
 930   *
 931   * @param int  $day
 932   *   The day of the month.
 933   * @param int  $month
 934   *   The month.
 935   * @param int  $year
 936   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
 937   * @param string $format
 938   *   The string indicating how to format the output.
 939   *
 940   * @return string
 941   *   The date in the desired format.
 942   */
 943  function date_calc_next_weekday($day = 0, $month = 0, $year = 0, $format = DATE_CALC_FORMAT) {
 944    if (empty($year)) {
 945      $year = date_calc_get_year();
 946    }
 947    if (empty($month)) {
 948      $month = date_calc_get_month();
 949    }
 950    if (empty($day)) {
 951      $day = date_calc_get_day();
 952    }
 953    $days = date_calc_date_to_days($day, $month, $year);
 954    if (date_dow($day, $month, $year) == 5) {
 955      $days += 3;
 956    }
 957    elseif (date_dow($day, $month, $year) == 6) {
 958      $days += 2;
 959    }
 960    else {
 961      $days += 1;
 962    }
 963    return date_calc_days_to_date($days, $format);
 964  }
 965  
 966  /**
 967   * Returns date of the previous specific day of the week
 968   * from the given date.
 969   *
 970   * @param int
 971   *   Day of week, 0=Sunday.
 972   * @param int  $day
 973   *   The day of the month.
 974   * @param int  $month
 975   *   The month.
 976   * @param int  $year
 977   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
 978   * @param bool   $on_or_before
 979   *   If true and days are same, returns current day.
 980   * @param string $format
 981   *   The string indicating how to format the output.
 982   *
 983   * @return string
 984   *   The date in the desired format.
 985   */
 986  function date_calc_prev_day_of_week($dow, $day = 0, $month = 0, $year = 0,
 987    $format = DATE_CALC_FORMAT, $on_or_before = false) {
 988    if (empty($year)) {
 989      $year = date_calc_get_year();
 990    }
 991    if (empty($month)) {
 992      $month = date_calc_get_month();
 993    }
 994    if (empty($day)) {
 995      $day = date_calc_get_day();
 996    }
 997    $days = date_calc_date_to_days($day, $month, $year);
 998    $curr_weekday = date_dow($day, $month, $year);
 999    if ($curr_weekday == $dow) {
1000      if (!$on_or_before) {
1001        $days -= 7;
1002      }
1003    }
1004    elseif ($curr_weekday < $dow) {
1005      $days -= 7 - ($dow - $curr_weekday);
1006    }
1007    else {
1008      $days -= $curr_weekday - $dow;
1009    }
1010    return date_calc_days_to_date($days, $format);
1011  }
1012  
1013  /**
1014   * Returns date of the next specific day of the week
1015   * from the given date.
1016   *
1017   * @param int  $dow
1018   *   The day of the week (0 = Sunday).
1019   * @param int  $day
1020   *   The day of the month.
1021   * @param int  $month
1022   *   The month.
1023   * @param int  $year
1024   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
1025   * @param bool   $on_or_after
1026   *   If true and days are same, returns current day.
1027   * @param string $format
1028   *   The string indicating how to format the output.
1029   *
1030   * @return string  the date in the desired format
1031   */
1032  function date_calc_next_day_of_week($dow, $day = 0, $month = 0, $year = 0,
1033    $format = DATE_CALC_FORMAT, $on_or_after = false) {
1034    if (empty($year)) {
1035      $year = date_calc_get_year();
1036    }
1037    if (empty($month)) {
1038      $month = date_calc_get_month();
1039    }
1040    if (empty($day)) {
1041      $day = date_calc_get_day();
1042    }
1043    $days = date_calc_date_to_days($day, $month, $year);
1044    $curr_weekday = date_dow($day, $month, $year);
1045    if ($curr_weekday == $dow) {
1046      if (!$on_or_after) {
1047        $days += 7;
1048      }
1049    }
1050    elseif ($curr_weekday > $dow) {
1051      $days += 7 - ($curr_weekday - $dow);
1052    }
1053    else {
1054      $days += $dow - $curr_weekday;
1055    }
1056    return date_calc_days_to_date($days, $format);
1057  }
1058  
1059  /**
1060   * Find the month day of the beginning of week for given date,
1061   * using variable_get('date_first_day', 1). Can return weekday of prev month.
1062   *
1063   * @param int  $day
1064   *   The day of the month.
1065   * @param int  $month
1066   *   The month.
1067   * @param int  $year
1068   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
1069   * @param string $format
1070   *   The string indicating how to format the output.
1071   *
1072   * @return string
1073   *   The date in the desired format.
1074   */
1075  function date_calc_begin_of_week($day = 0, $month = 0, $year = 0,
1076    $format = DATE_CALC_FORMAT) {
1077    if (empty($year)) {
1078      $year = date_calc_get_year();
1079    }
1080    if (empty($month)) {
1081      $month = date_calc_get_month();
1082    }
1083    if (empty($day)) {
1084      $day = date_calc_get_day();
1085    }
1086    $this_weekday = date_dow($day, $month, $year);
1087    $interval = (7 - variable_get('date_first_day', 1) + $this_weekday) % 7;
1088    return date_calc_days_to_date(date_calc_date_to_days($day, $month, $year)
1089      - $interval, $format);
1090  }
1091  
1092  /**
1093   * Find the month day of the end of week for given date, using
1094   * variable_get('date_first_day', 1). Can return weekday of following month.
1095   *
1096   * @param int  $day
1097   *   The day of the month.
1098   * @param int  $month
1099   *   The month.
1100   * @param int  $year
1101   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
1102   * @param string $format
1103   *   The string indicating how to format the output.
1104   *
1105   * @return string
1106   *   The date in the desired format.
1107   */
1108  function date_calc_end_of_week($day = 0, $month = 0, $year = 0,
1109    $format = DATE_CALC_FORMAT) {
1110    if (empty($year)) {
1111      $year = date_calc_get_year();
1112    }
1113    if (empty($month)) {
1114      $month = date_calc_get_month();
1115    }
1116    if (empty($day)) {
1117      $day = date_calc_get_day();
1118    }
1119    $this_weekday = date_dow($day, $month, $year);
1120    $interval = (6 + variable_get('date_first_day', 1) - $this_weekday) % 7;
1121    return date_calc_days_to_date(date_calc_date_to_days($day, $month, $year)
1122      + $interval, $format);
1123  }
1124  
1125  /**
1126   * Find the month day of the beginning of week before given date, using
1127   * variable_get('date_first_day', 1). Can return weekday of prev month.
1128   *
1129   * @param int  $day
1130   *   The day of the month.
1131   * @param int  $month
1132   *   The month.
1133   * @param int  $year
1134   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
1135   * @param string $format
1136   *   The string indicating how to format the output.
1137   *
1138   * @return string
1139   *   The date in the desired format.
1140   */
1141  function date_calc_begin_of_prev_week($day = 0, $month = 0, $year = 0, $format = DATE_CALC_FORMAT) {
1142    if (empty($year)) {
1143      $year = date_calc_get_year();
1144    }
1145    if (empty($month)) {
1146      $month = date_calc_get_month();
1147    }
1148    if (empty($day)) {
1149      $day = date_calc_get_day();
1150    }
1151    $date = date_calc_days_to_date(date_calc_date_to_days($day-7, $month, $year), '%Y%m%d');
1152    $prev_week_year  = intval(substr($date, 0, 4));
1153    $prev_week_month = intval(substr($date, 4, 2));
1154    $prev_week_day   = intval(substr($date, 6, 2));
1155    return date_calc_begin_of_week($prev_week_day, $prev_week_month, $prev_week_year, $format);
1156  }
1157  
1158  /**
1159   * Find the month day of the beginning of week after given date, using
1160   * variable_get('date_first_day', 1). Can return weekday of next month.
1161   *
1162   * @param int  $day
1163   *   The day of the month.
1164   * @param int  $month
1165   *   The month.
1166   * @param int  $year
1167   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
1168   * @param string $format
1169   *   The string indicating how to format the output.
1170   *
1171   * @return string  the date in the desired format
1172   */
1173  function date_calc_begin_of_next_week($day = 0, $month = 0, $year = 0,
1174    $format = DATE_CALC_FORMAT) {
1175    if (empty($year)) {
1176      $year = date_calc_get_year();
1177    }
1178    if (empty($month)) {
1179      $month = date_calc_get_month();
1180    }
1181    if (empty($day)) {
1182      $day = date_calc_get_day();
1183    }
1184    $date = date_calc_days_to_date(date_calc_date_to_days($day + 7, $month, $year), '%Y%m%d');
1185    $next_week_year  = intval(substr($date, 0, 4));
1186    $next_week_month = intval(substr($date, 4, 2));
1187    $next_week_day   = intval(substr($date, 6, 2));
1188    return date_calc_begin_of_week($next_week_day, $next_week_month, $next_week_year, $format);
1189  }
1190  
1191  /**
1192   * Returns date of the first day of the month in the number of months
1193   * from the given date
1194   *
1195   * @param int  $months
1196   *   The number of months from the date provided.
1197   *   Positive numbers go into the future.
1198   *   Negative numbers go into the past.
1199   *   0 is the month presented in $month.
1200   * @param int  $month
1201   *   The month.
1202   * @param int  $year
1203   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
1204   * @param string $format
1205   *   The string indicating how to format the output.
1206   *
1207   * @return string
1208   *   The date in the desired format.
1209   */
1210  function date_calc_begin_of_month_by_span($months = 0, $month = 0, $year = 0, $format = DATE_CALC_FORMAT) {
1211    if (empty($year)) {
1212      $year = date_calc_get_year();
1213    }
1214    if (empty($month)) {
1215      $month = date_calc_get_month();
1216    }
1217    if ($months > 0) {
1218      // future month
1219      $tmp_mo = $month + $months;
1220      $month  = $tmp_mo % 12;
1221      if ($month == 0) {
1222        $month = 12;
1223        $year = $year + floor(($tmp_mo - 1) / 12);
1224      }
1225      else {
1226        $year = $year + floor($tmp_mo / 12);
1227      }
1228    }
1229    else {
1230      // past or present month
1231      $tmp_mo = $month + $months;
1232      if ($tmp_mo > 0) {
1233        // same year
1234        $month = $tmp_mo;
1235      }
1236      elseif ($tmp_mo == 0) {
1237        // prior dec
1238        $month = 12;
1239        $year--;
1240      }
1241      else {
1242        // some time in a prior year
1243        $month = 12 + ($tmp_mo % 12);
1244        $year  = $year + floor($tmp_mo / 12);
1245      }
1246    }
1247    return date_calc_format(1, $month, $year, $format);
1248  }
1249  
1250  /**
1251   * Returns date of the last day of the month in the number of months
1252   * from the given date.
1253   *
1254   * @param int  $months
1255   *   The number of months from the date provided.
1256   *   Positive numbers go into the future.
1257   *   Negative numbers go into the past.
1258   *   0 is the month presented in $month.
1259   * @param int  $month
1260   *   The month.
1261   * @param int  $year
1262   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
1263   * @param string $format
1264   *   The string indicating how to format the output.
1265   *
1266   * @return string
1267   *   The date in the desired format.
1268   */
1269  function date_calc_end_of_month_by_span($months = 0, $month = 0, $year = 0, $format = DATE_CALC_FORMAT) {
1270    if (empty($year)) {
1271      $year = date_calc_get_year();
1272    }
1273    if (empty($month)) {
1274      $month = date_calc_get_month();
1275    }
1276    if ($months > 0) {
1277      // future month
1278      $tmp_mo = $month + $months;
1279      $month  = $tmp_mo % 12;
1280      if ($month == 0) {
1281        $month = 12;
1282        $year = $year + floor(($tmp_mo - 1) / 12);
1283      }
1284      else {
1285        $year = $year + floor($tmp_mo / 12);
1286      }
1287    }
1288    else {
1289      // past or present month
1290      $tmp_mo = $month + $months;
1291      if ($tmp_mo > 0) {
1292        // same year
1293        $month = $tmp_mo;
1294      }
1295      elseif ($tmp_mo == 0) {
1296        // prior dec
1297        $month = 12;
1298        $year--;
1299      }
1300      else {
1301        // some time in a prior year
1302        $month = 12 + ($tmp_mo % 12);
1303        $year  = $year + floor($tmp_mo / 12);
1304      }
1305    }
1306    return date_calc_format(date_calc_days_in_month($month, $year), $month, $year, $format);
1307  }
1308  
1309  /**
1310   * Find the day of the week for the first of the month of given date
1311   *
1312   * @param int  $month
1313   *   The month.
1314   * @param int  $year
1315   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
1316   *
1317   * @return int
1318   *   Number of weekday for the first day, 0=Sunday.
1319   */
1320  function date_calc_first_of_month_weekday($month = 0, $year = 0) {
1321    if (empty($year)) {
1322      $year = date_calc_get_year();
1323    }
1324    if (empty($month)) {
1325      $month = date_calc_get_month();
1326    }
1327    return date_dow('01', $month, $year);
1328  }
1329  
1330  /**
1331   * Calculates the date of the Nth weekday of the month,
1332   * such as the second Saturday of January 2000
1333   *
1334   * @param int  $week
1335   *   The number of the week to get (1 = first, etc.  Also can be 'last'.)
1336   * @param int  $dow
1337   *   The day of the week (0 = Sunday).
1338   * @param int  $month
1339   *   The month.
1340   * @param int  $year
1341   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
1342   * @param string $format
1343   *   The string indicating how to format the output.
1344   *
1345   * @return string
1346   *   The date in the desired format.
1347   */
1348  function date_calc_n_weekday_of_month($week, $dow, $month, $year, $format = DATE_CALC_FORMAT) {
1349    if (is_numeric($week)) {
1350      $DOW1day = ($week - 1) * 7 + 1;
1351      $DOW1  = date_dow($DOW1day, $month, $year);
1352      $wdate   = ($week - 1) * 7 + 1 + (7 + $dow - $DOW1) % 7;
1353      if ($wdate > date_calc_days_in_month($month, $year)) {
1354        return -1;
1355      }
1356      else {
1357        return date_calc_format($wdate, $month, $year, $format);
1358      }
1359    }
1360    elseif ($week == 'last' && $dow < 7) {
1361      $lastday = date_calc_days_in_month($month, $year);
1362      $lastdow = date_dow($lastday, $month, $year);
1363      $diff  = $dow - $lastdow;
1364      if ($diff > 0) {
1365        return date_calc_format($lastday - (7 - $diff), $month, $year, $format);
1366      }
1367      else {
1368        return date_calc_format($lastday + $diff, $month, $year, $format);
1369      }
1370    }
1371    else {
1372      return -1;
1373    }
1374  }
1375  
1376  /**
1377   * Determines if given date is a future date from now
1378   *
1379   * @param int  $day
1380   *   The day of the month.
1381   * @param int  $month
1382   *   The month.
1383   * @param int  $year
1384   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
1385   *
1386   * @return boolean
1387   */
1388  function date_calc_is_future_date($day, $month, $year) {
1389    $this_year  = date_calc_get_year();
1390    $this_month = date_calc_get_month();
1391    $this_day   = date_calc_get_day();
1392    if ($year > $this_year) {
1393      return true;
1394    }
1395    elseif ($year == $this_year) {
1396      if ($month > $this_month) {
1397        return true;
1398      }
1399      elseif ($month == $this_month) {
1400        if ($day > $this_day) {
1401          return true;
1402        }
1403      }
1404    }
1405    return false;
1406  }
1407  
1408  /**
1409   * Determines if given date is a past date from now
1410   *
1411   * @param int  $day
1412   *   the day of the month
1413   * @param int  $month
1414   *   the month
1415   * @param int  $year
1416   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
1417   *
1418   * @return boolean
1419   */
1420  function date_calc_is_past_date($day, $month, $year) {
1421    $this_year  = date_calc_get_year();
1422    $this_month = date_calc_get_month();
1423    $this_day   = date_calc_get_day();
1424    if ($year < $this_year) {
1425      return true;
1426    }
1427    elseif ($year == $this_year) {
1428      if ($month < $this_month) {
1429        return true;
1430      }
1431      elseif ($month == $this_month) {
1432        if ($day < $this_day) {
1433          return true;
1434        }
1435      }
1436    }
1437    return false;
1438  }
1439  
1440  /**
1441   * Returns number of days between two given dates
1442   *
1443   * @param int  $day1
1444   *   the day of the month
1445   * @param int  $month1
1446   *   the month
1447   * @param int  $year1
1448   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
1449   * @param int  $day2
1450   *   the day of the month
1451   * @param int  $month2
1452   *   the month
1453   * @param int  $year2
1454   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
1455   *
1456   * @return int
1457   *   the absolute number of days between the two dates.
1458   *   If an error occurs, -1 is returned.
1459   */
1460  function date_calc_date_diff($day1, $month1, $year1, $day2, $month2, $year2) {
1461    if (!date_calc_is_valid($day1, $month1, $year1)) {
1462      return -1;
1463    }
1464    if (!date_calc_is_valid($day2, $month2, $year2)) {
1465      return -1;
1466    }
1467    return abs(date_calc_date_to_days($day1, $month1, $year1)
1468      - date_calc_date_to_days($day2, $month2, $year2));
1469  }
1470  
1471  /**
1472   * Compares two dates
1473   *
1474   * @param int  $day1
1475   *   the day of the month
1476   * @param int  $month1
1477   *   the month
1478   * @param int  $year1
1479   *   The 4 digit year. Do not add leading 0's for years prior to 1000.
1480   * @param int  $day2
1481   *   the day of the month
1482   * @param int  $month2
1483   *   the month
1484   * @param int  $year2
1485   *   the year.  Use the complete year instead of the abbreviated version.
1486   *   E.g. use 2005, not 05. Do not add leading 0's for years prior to 1000.
1487   *
1488   * @return int
1489   *   0 if the dates are equal. 1 if date 1 is later, -1 if date 1 is earlier.
1490   */
1491  function date_calc_compare_dates($day1, $month1, $year1, $day2, $month2, $year2) {
1492    $ndays1 = date_calc_date_to_days($day1, $month1, $year1);
1493    $ndays2 = date_calc_date_to_days($day2, $month2, $year2);
1494    if ($ndays1 == $ndays2) {
1495      return 0;
1496    }
1497    return ($ndays1 > $ndays2) ? 1 : -1;
1498  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7