如何使日期时间类与DST一起工作,而不使用PHP中的DST?

问题描述:

I have wrote the code where i am using DateTime class which also converts dst following countries time into GMT format. But i want the functionality where i need the DateTime class should work on checkbox selection. when a user checks dst then only that functionality should work. here my code goes........

function ConvertOneTimezoneToAnotherTimezone($time,$currentTimezone,$timezoneRequired)
{

     $current_zone = new DateTimeZone($currentTimezone);
    // print_r($current_zone);
     //$gmt = new DateTimeZone('GMT');

     $date = new DateTime($time, $current_zone);
    //var_dump($date);
     //$date->setTimezone($gmt);
     $date->setTimezone(new DateTimeZone($timezoneRequired));

     return  $date->format('Y-m-d H:i:s');

    // Convert it back to Original timezone
     //$date->setTimezone($current_zone);
     //return  $date->format('Y-m-d H:i:s');
 }

 $time='2011-03-29 16:07:00.000';

 echo  "Current Date/Time is=".ConvertOneTimezoneToAnotherTimezone($time,'Asia/Kolkata','UTC');

................ please help me.....how to make this to work.......

我编写了代码,其中我使用的是DateTime类,它还将dst跟随国家/地区的时间转换为GMT格式。 But 我想要我需要DateTime类的功能应该在复选框选择上工作。 当用户检查dst然后只有那个功能应该工作。\我的代码去........ p>

  function ConvertOneTimezoneToAnotherTimezone($ time,$ currentTimezone,$ timezoneRequired)
 {
 
 $ current_zone = new DateTimeZone($ currentTimezone); 
 // print_r($ current_zone); 
 // $  gmt = new DateTimeZone('GMT'); 
 
 $ date = new DateTime($ time,$ current_zone); 
 // var_dump($ date); 
 // $ date-> setTimezone($ gmt  ); 
 $ date-> setTimezone(new DateTimeZone($ timezoneRequired)); 
 
返回$ date->格式('Ymd H:i:s'); 
 
 //将其转换回来 到原始时区
 // $ date-> setTimezone($ current_zone); 
 //返回$ date->格式('Ymd H:i:s'); 
} 
 
 $ time =  '2011-03-29 16:0  7:00.000'; 
 
 echo“Current Date / Time is =”.ConvertOneTimezoneToAnotherTimezone($ time,'Asia / Kolkata','UTC'); 
  code>  pre> 
 
  ................ 
请帮助我.....如何使这个工作.......  p> 
  div>

Try this code and let me know does it work?

function ConvertOneTimezoneToAnotherTimezone($time,$currentTimezone,$timezoneRequired,$considerDST=true) {
  // save current timezone
  $backup_tz = date_default_timezone_get();
  date_default_timezone_set($currentTimezone);
  $t = strtotime($time);
  date_default_timezone_set($timezoneRequired);
  if (!$considerDST && (date('I', $t) == 1)) {
    if ($timezoneRequired == 'Australia/Lord_Howe') $dst='-30 minutes';
    else $dst = "-1 hour";
    $t = strtotime($dst, $t);
    }
  // restore old timezone
  $res = date('Y-m-d H:i:s', $t);
  date_default_timezone_set($backup_tz);
  return $res;
  }

$mytime = '2011-03-29 12:40:00.000';
$myzone = 'UTC';

echo ConvertOneTimezoneToAnotherTimezone($mytime, $myzone, 'Australia/Adelaide', true) . " (Adelaide DST=Yes)<br>";
echo ConvertOneTimezoneToAnotherTimezone($mytime, $myzone, 'Australia/Adelaide', false) . " (Adelaide  DST=No)";

Additional parameter $considerDST is boolean so pass true (or skip this parameter; its default) if you want DST or false if you don't.

Man if you want to convert times to UTC... you need to pass same time as input params of the function.

YOU CANT DO THIS!

$mytime = '2011-03-30 12:52:00.000';
$myzone = 'Europe/Belgrade';
...
$mytime = '2011-03-30 12:52:00.000';
$myzone = 'America/New_York';

and expect same UTC because it snot same time... When it is 12:52 in Belgrade, its 06:52 in NewYork, so...

2011-03-20 12:52:00.000 [Europe/Belgrade]
2011-03-30 06:52:00.000 [America/NewYork]
2011-03-30 16:22:00.000 [Asia/Calcutta] or [Asia/Kolkata]
2011-03-30 19:52:00.000 [Asia/Pyongyang]
etc...

has same UTC time 2011-03-20 10:52:00.000

FROM MY CODE...

$mytime = '2011-03-30 21:00:00.000';
$myzone = 'Australia/Melbourne';

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


$mytime = '2011-03-30 15:30:00.000';
$myzone = 'Asia/Kolkata';

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

THATS SAME UTC 10:00... I don't understand! Tell me what are you doing with your code again?

OUTPUT:

2011-03-30 10:00:00 (Melbourne->UTC DST=Yes)
2011-03-30 10:00:00 (Melbourne->UTC DST=No)

2011-03-30 10:00:00 (India->UTC DST=Yes)
2011-03-30 10:00:00 (India->UTC DST=No)