dongshungai4857 2011-03-29 11:00
浏览 93
已采纳

如何使日期时间类与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.......

  • 写回答

1条回答 默认 最新

  • drkbpwk609294 2011-03-29 12:53
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值