You are here

function date_is_dst in Date 6.2

Same name and namespace in other branches
  1. 5.2 date_php4/date_php4.inc \date_is_dst()
  2. 5 date_timezones.inc \date_is_dst()
  3. 6 date_php4/date_php4.inc \date_is_dst()

Check if time is in Daylight Savings Time Uses dst region code, as stored in the $timezone_map array from date_php4_tz_map.inc

Parameters

$timezone name:

$timestamp:

Return value

0 or 1

1 call to date_is_dst()
date_php4.inc in date_php4/date_php4.inc

File

date_php4/date_php4.inc, line 609

Code

function date_is_dst($timestamp, $timezone = NULL) {
  if (abs($timestamp) > 0x7fffffff) {
    return FALSE;
  }
  if (empty($timezone)) {
    $timezone = date_default_timezone_name();
  }
  static $timezone_map = array();
  if (empty($timezone_map)) {
    include drupal_get_path('module', 'date_php4') . '/date_php4_tz_map.inc';
  }
  $region = array_key_exists($timezone, $timezone_map) ? $timezone_map[$timezone]['dst_region'] : 0;

  // This should really be done with date_date() to get the right timezone
  // adjustment for the year, but that leads to circular code that won't
  // work, so we're stuck with date(), which hopefully will create the right
  // year in most cases.
  $year = date('Y', $timestamp);

  // Information on Daylight Saving time was obtained from http://webexhibits.org/daylightsaving/g.html
  // Note that 'last sunday' is interpreted as 'the last sunday before...'.
  switch ($region) {
    case 0:
      return 0;
    case 1:

      // Egypt
      // start of DST (last Friday in April)
      $dststart = strtotime("last friday UTC", strtotime("1 may {$year} UTC"));

      // end of DST (last Thursday in September)
      $dstend = strtotime("last thursday UTC", strtotime("1 october {$year} UTC"));
      break;
    case 2:

      // Namibia
      // start of DST (first Sunday in September)
      $dststart = strtotime("first sunday UTC", strtotime("1 september {$year} UTC"));

      // end of DST (first Sunday April)
      $dstend = strtotime("first sunday UTC", strtotime("1 april {$year} UTC"));
      break;
    case 3:

      // Former USSR
      // start of DST (last Sunday in March)
      $dststart = strtotime("last sunday UTC", strtotime("1 april {$year} UTC"));

      // end of DST (last Sunday October)
      $dstend = strtotime("last sunday UTC", strtotime("1 november {$year} UTC"));
      break;
    case 4:

      // Iraq, Syria
      // start of DST (April 1st)
      $dststart = strtotime("1 april {$year} UTC");

      // end of DST (October 1st)
      $dstend = strtotime("1 october {$year} UTC");
      break;
    case 5:

      // Israel
      // start of DST (last Friday befor April 2nd, 2am)
      $dststart = strtotime("-1 week friday GMT", strtotime("2 april {$year} GMT")) + 7200;

      // end of DST (last sunday before 10th of tishray. see http://cgate.co.il/navigation/shaon_kaiz.htm.
      switch ($year) {
        case '2007':
          $dstend = strtotime("-1 sunday GMT", strtotime("16 september {$year} GMT")) + 7200;
          break;
        case '2008':
          $dstend = strtotime("-1 sunday GMT", strtotime("9 october {$year} GMT")) + 7200;
          break;
        case '2009':
          $dstend = strtotime("-1 sunday GMT", strtotime("28 september {$year} GMT")) + 7200;
          break;
        case '2010':
          $dstend = strtotime("-1 sunday GMT", strtotime("18 september {$year} GMT")) + 7200;
          break;
        case '2011':
          $dstend = strtotime("-1 sunday GMT", strtotime("8 october {$year} GMT")) + 7200;
          break;
        case '2012':
          $dstend = strtotime("-1 sunday GMT", strtotime("26 september {$year} GMT")) + 7200;
          break;
        case '2013':
          $dstend = strtotime("-1 sunday GMT", strtotime("14 september {$year} GMT")) + 7200;
          break;
        case '2014':
          $dstend = strtotime("-1 sunday GMT", strtotime("4 october {$year} GMT")) + 7200;
          break;
        case '2015':
          $dstend = strtotime("-1 sunday GMT", strtotime("23 september {$year} GMT")) + 7200;
          break;
        case '2016':
          $dstend = strtotime("-1 sunday GMT", strtotime("12 october {$year} GMT")) + 7200;
          break;
        default:
          return FALSE;
      }
      break;
    case 6:

      // Lebanon, Kirgizstan
      // start of DST (Last Sunday in March)
      $dststart = strtotime("last sunday UTC", strtotime("1 april {$year} UTC"));

      // end of DST (Last Sunday in October)
      $dstend = strtotime("last sunday UTC", strtotime("1 november {$year} UTC"));
      break;
    case 7:

      // Palestine
      // start of DST (First Friday on or after April 15th)
      $dststart = strtotime("next friday UTC", strtotime("14 april {$year} UTC"));

      // end of DST (First Friday on or after October 15th)
      $dstend = strtotime("next friday UTC", strtotime("14 october {$year} UTC"));
      break;
    case 8:

      // Iran
      // start of DST (the first day of Farvardin (March 21))
      $dststart = strtotime("21 march {$year} UTC");

      // end of DST (the first day of Mehr (September 23))
      $dstend = strtotime("23 september {$year} UTC");
      break;
    case 9:

      // South Australia
      // start of DST  (last Sunday in October)
      $dststart = strtotime("last sunday UTC", strtotime("1 november {$year} UTC"));

      // end of DST (last Sunday in March)
      $dstend = strtotime("last sunday UTC", strtotime("1 april {$year} UTC"));
      break;
    case 10:

      // Australia, Tasmania
      // start of DST  (first Sunday in October)
      $dststart = strtotime("first sunday UTC", strtotime("1 october {$year} UTC"));

      // end of DST (last Sunday in March)
      $dstend = strtotime("last sunday UTC", strtotime("1 april {$year} UTC"));
      break;
    case 11:

      // New Zealand
      // start of DST  (first Sunday in October)
      $dststart = strtotime("first sunday UTC", strtotime("1 october {$year} UTC"));

      // end of DST (first Sunday in April)
      $dstend = strtotime("first sunday UTC", strtotime("1 april {$year} UTC"));
      break;
    case 12:

      // Tonga
      // start of DST (first Sunday in November)
      $dststart = strtotime("first sunday UTC", strtotime("1 november {$year} UTC"));

      // end of DST (last Sunday in January)
      $dstend = strtotime("last sunday UTC", strtotime("1 february {$year} UTC"));
      break;
    case 13:

      // EU and other European countries
      // start of DST (last Sunday in March 1 am UTC)
      $dststart = strtotime("last sunday UTC", strtotime("1 april {$year} UTC"));

      // end of DST in Europe (last Sunday in October 1 am UTC)
      $dstend = strtotime("last sunday UTC", strtotime("1 november {$year} UTC"));
      break;
    case 14:

      // Russian Federation
      // start of DST (last Sunday in March 2 am local time)
      $dststart = strtotime("last sunday UTC", strtotime("1 april {$year} UTC"));

      // end of DST (last Sunday in October 2 am local time)
      $dstend = strtotime("last sunday UTC", strtotime("1 november {$year} UTC"));
      break;
    case 15:

      // Northern America (where applicable)
      // start of DST  (where applicable) (first Sunday in April before 2007,
      // after that second Sunday in March, 2 am local time)
      if ($year < 2007) {
        $dststart = strtotime("first sunday UTC", strtotime("1 april {$year} UTC"));
      }
      else {
        $dststart = strtotime("second sunday UTC", strtotime("1 march {$year} UTC"));
      }

      // end of DST (where applicable) (last Sunday in October 2 am local time)
      if ($year < 2007) {
        $dstend = strtotime("last sunday UTC", strtotime("1 november {$year} UTC"));
      }
      else {
        $dstend = strtotime("first sunday UTC", strtotime("1 november {$year} UTC"));
      }
      break;
    case 16:

      // Cuba
      // start of DST  (April 1st)
      $dststart = strtotime("1 april {$year} UTC");

      // end of DST (last Sunday in October)
      $dstend = strtotime("last sunday UTC", strtotime("1 november {$year} UTC"));
      break;
    case 17:

      // Brazil
      // start of DST  (first Sunday in November)
      $dststart = strtotime("first sunday UTC", strtotime("1 november {$year} UTC"));

      // end of DST (third Sunday in February)
      $dstend = strtotime("third sunday UTC", strtotime("1 february {$year} UTC"));
      break;
    case 18:

      // Chile
      // start of DST  (Second Saturday of October - at midnight)
      $dststart = strtotime("second saturday UTC", strtotime("1 october {$year} UTC"));

      // end of DST (Second Saturday of March - at midnight)
      $dstend = strtotime("second sunday UTC", strtotime("1 march {$year} UTC"));
      break;
    case 19:

      // Falklands
      // start of DST  (First Sunday on or after 8 September)
      $dststart = strtotime("next sunday UTC", strtotime("7 september {$year} UTC"));

      // end of DST (First Sunday on or after 6 April)
      $dstend = strtotime("next sunday UTC", strtotime("5 april {$year} UTC"));
      break;
    case 20:

      // Paraguay
      // start of DST  (first Sunday in September)
      $dststart = strtotime("first sunday UTC", strtotime("1 september {$year} UTC"));

      // end of DST (first Sunday in April)
      $dstend = strtotime("first sunday UTC", strtotime("1 april {$year} UTC"));
      break;
  }

  // Have to use reverse logic in southern hemisphere.
  $southern = array(
    9,
    10,
    11,
    12,
    17,
    18,
    19,
    20,
  );
  if (in_array($region, $southern)) {
    return !($timestamp <= $dststart && $dstend <= $timestamp);
  }
  else {
    return $dststart <= $timestamp && $timestamp <= $dstend;
  }
}