You are here

function _date_getdate in Date 6.2

Same name and namespace in other branches
  1. 5.2 date_php4/date_php4_lib.inc \_date_getdate()
  2. 6 date_php4/date_php4_lib.inc \_date_getdate()

Low-level function that returns the getdate() array for pre-1970 and post-2038 dates.

We have a special$fast flag, which if set to true, will return fewer array values, and is much faster as it does not calculate dow, etc.

Parameters

$timestamp a unix timestamp:

$timezone name: Use 'UTC' to avoid timezone conversion.

2 calls to _date_getdate()
date_getdate in date_php4/date_php4.inc
Returns an array with date info.
_date_date in date_php4/date_php4_lib.inc
Low level function to create date() for pre-1970 and post-2038 dates.

File

date_php4/date_php4_lib.inc, line 64

Code

function _date_getdate($timestamp = FALSE, $timezone = FALSE) {
  static $YRS;
  if ($timezone === FALSE) {
    $timezone = date_default_timezone_name();
  }
  $timestamp_in = $timestamp;
  $_day_power = 86400;
  $_hour_power = 3600;
  $_min_power = 60;
  if ($timestamp < -12219321600) {
    $timestamp -= 86400 * 10;
  }

  // if 15 Oct 1582 or earlier, gregorian correction
  $_month_table_normal = array(
    "",
    31,
    28,
    31,
    30,
    31,
    30,
    31,
    31,
    30,
    31,
    30,
    31,
  );
  $_month_table_leap = array(
    "",
    31,
    29,
    31,
    30,
    31,
    30,
    31,
    31,
    30,
    31,
    30,
    31,
  );
  $d366 = $_day_power * 366;
  $d365 = $_day_power * 365;
  if ($timestamp < 0) {
    if (empty($YRS)) {
      $YRS = array(
        1970 => 0,
        1960 => -315619200,
        1950 => -631152000,
        1940 => -946771200,
        1930 => -1262304000,
        1920 => -1577923200,
        1910 => -1893456000,
        1900 => -2208988800,
        1890 => -2524521600,
        1880 => -2840140800,
        1870 => -3155673600,
        1860 => -3471292800,
        1850 => -3786825600,
        1840 => -4102444800,
        1830 => -4417977600,
        1820 => -4733596800,
        1810 => -5049129600,
        1800 => -5364662400,
        1790 => -5680195200,
        1780 => -5995814400,
        1770 => -6311347200,
        1760 => -6626966400,
        1750 => -6942499200,
        1740 => -7258118400,
        1730 => -7573651200,
        1720 => -7889270400,
        1710 => -8204803200,
        1700 => -8520336000,
        1690 => -8835868800,
        1680 => -9151488000,
        1670 => -9467020800,
        1660 => -9782640000,
        1650 => -10098172800,
        1640 => -10413792000,
        1630 => -10729324800,
        1620 => -11044944000,
        1610 => -11360476800,
        1600 => -11676096000,
      );
    }

    // The valid range of a 32bit signed timestamp is typically from
    // Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT
    //
    $lastsecs = 0;
    $lastyear = 1970;
    foreach ($YRS as $year => $secs) {
      if ($timestamp >= $secs) {
        $a = $lastyear;
        break;
      }
      $lastsecs = $secs;
      $lastyear = $year;
    }
    $timestamp -= $lastsecs;
    if (!isset($a)) {
      $a = $lastyear;
    }
    for (; --$a >= 0;) {
      $lastd = $timestamp;
      if ($leap = date_is_leap_year($a)) {
        $timestamp += $d366;
      }
      else {
        $timestamp += $d365;
      }
      if ($timestamp >= 0) {
        $year = $a;
        break;
      }
    }
    $secs_in_year = 86400 * ($leap ? 366 : 365) + $lastd;
    $timestamp = $lastd;
    $mtab = $leap ? $_month_table_leap : $_month_table_normal;
    for ($a = 13; --$a > 0;) {
      $lastd = $timestamp;
      $timestamp += $mtab[$a] * $_day_power;
      if ($timestamp >= 0) {
        $month = $a;
        $ndays = $mtab[$a];
        break;
      }
    }
    $timestamp = $lastd;
    $day = $ndays + ceil(($timestamp + 1) / $_day_power);
    $timestamp += ($ndays - $day + 1) * $_day_power;
    $hour = floor($timestamp / $_hour_power);
  }
  else {
    if ($timezone != 'UTC') {
      $timestamp += date_get_gmt_diff_ts($timestamp, $timezone);
    }
    for ($a = 1970;; $a++) {
      $lastd = $timestamp;
      if ($leap = date_is_leap_year($a)) {
        $timestamp -= $d366;
      }
      else {
        $timestamp -= $d365;
      }
      if ($timestamp < 0) {
        $year = $a;
        break;
      }
    }
    $secs_in_year = $lastd;
    $timestamp = $lastd;
    $mtab = $leap ? $_month_table_leap : $_month_table_normal;
    for ($a = 1; $a <= 12; $a++) {
      $lastd = $timestamp;
      $timestamp -= $mtab[$a] * $_day_power;
      if ($timestamp < 0) {
        $month = $a;
        $ndays = $mtab[$a];
        break;
      }
    }
    $timestamp = $lastd;
    $day = ceil(($timestamp + 1) / $_day_power);
    $timestamp = $timestamp - ($day - 1) * $_day_power;
    $hour = floor($timestamp / $_hour_power);
  }
  $timestamp -= $hour * $_hour_power;
  $min = floor($timestamp / $_min_power);
  $secs = $timestamp - $min * $_min_power;
  $dow = date_dow($day, $month, $year);
  return array(
    'second' => $secs,
    'minute' => $min,
    'hour' => $hour,
    'day' => $day,
    'wday' => $dow,
    'month' => $month,
    'year' => $year,
    'yday' => floor($secs_in_year / $_day_power),
    'weekday' => gmdate('l', $_day_power * (3 + $dow)),
    'leap' => $leap,
    'ndays' => $ndays,
    'month_name' => gmdate('F', mktime(0, 0, 0, $month, 2, 1971)),
    0 => $timestamp_in,
  );
}