开发者

checking for timezones which dont follow DST?

My code works fine for converting any timezone into GMT/UTC and vice-versa. But i want the functionality where it should also check for those timezones which are not following DST and also should include the functionality where the date/time can be in any format.

Means if simply date is entered it should give me the correct result. For eample:

If i entered ju开发者_如何学Gost '2011-03-31' or date in any format it it should give me the correct result. This is my code.

function ConvertOneTimezoneToAnotherTimezone($time,$currentTimezone,$timezoneRequired,$requried_DST=true) {

  date_default_timezone_set($currentTimezone);
  $current_time = strtotime($time);
  date_default_timezone_set($timezoneRequired);

  if (!$requried_DST && (date('I', $current_time) == 1))
   {
          if ($timezoneRequired == 'Australia/Lord_Howe')
             $dst='-30 minutes';
          else $dst = "-1 hour";
        $current_time = strtotime($dst, $current_time);

    }
          // restore old timezone
          $res = date('Y-m-d H:i:s', $current_time);
          return $res;
  }

please anybody help me.

here is an example ...... my inputs are........

    $mytime = '2011-03-31 2:35:00.000';
    $myzone = 'America/New_York';

    echo  "(New_York->UTC DST=Yes)".ConvertOneTimezoneToAnotherTimezone($mytime, $myzone, 'UTC', true) ."<br>";
    echo "(New_York->UTC DST=No)".ConvertOneTimezoneToAnotherTimezone($mytime, $myzone, 'UTC', false) . " <br><br>";

///////////////////////

$mytime = '2011-03-31 6:35:00.000';
$myzone = 'UTC';

echo  "(UTC->New_York DST=Yes)".ConvertOneTimezoneToAnotherTimezone($mytime, $myzone, 'America/New_York', true) ."<br>";
echo "(UTC->New_York DST=No)".ConvertOneTimezoneToAnotherTimezone($mytime, $myzone, 'America/New_York', false) . " <br><br>";

and the result is:

(New_York->UTC DST=Yes)2011-03-31 06:35:00
(New_York->UTC DST=No)2011-03-31 06:35:00

(UTC->New_York DST=Yes)2011-03-31 02:35:00
(UTC->New_York DST=No)2011-03-31 01:35:00

............here date entered can be in anyformat...........


The most simple way to do this should be (although I don't understand your $requried_DST switch)

function ConvertOneTimezoneToAnotherTimezone($time, $currentTimezone, $timezoneRequired) {
    $dt = new DateTime($time, new DateTimeZone($currentTimezone));
    $dt->setTimezone(new DateTimeZone($timezoneRequired));
    return $dt->format('Y-m-d H:i:s');
}

Example:

echo ConvertOneTimezoneToAnotherTimezone('2011-03-31 12:00', 'Europe/Berlin', 'UTC');
// 2011-03-31 10:00:00 that's two hours difference because of DST
echo ConvertOneTimezoneToAnotherTimezone('2011-03-12 12:00', 'Europe/Berlin', 'UTC');
// 2011-03-12 11:00:00 that was before DST transition last weekend
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜