【问题标题】:how to make datetime class to work with DST and without DST in php?如何使 datetime 类在 php 中使用 DST 而没有 DST?
【发布时间】:2011-07-25 04:39:53
【问题描述】:

我已经编写了使用 DateTime 类的代码,该类还将 dst 以下国家时间转换为 GMT 格式。 但我希望我需要 DateTime 类的功能应该适用于复选框选择。 当用户检查 dst 时,只有该功能才能工作。 我的代码在这里............

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');

....... 请帮助我.....如何使它工作.......

【问题讨论】:

    标签: php date timezone gmt


    【解决方案1】:

    试试这个代码,让我知道它是否有效?

    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)";
    

    附加参数 $considerDST 是布尔值,因此如果需要 DST,则传递 true(或跳过此参数;它的默认值),否则传递 false。

    如果您想将时间转换为 UTC……您需要传递与函数的输入参数相同的时间。

    你不能这样做!

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

    并期待相同的 UTC,因为它不是同一时间......当贝尔格莱德是 12:52 时,在纽约是 06:52,所以......

    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...
    

    具有相同的 UTC 时间 2011-03-20 10:52:00.000

    来自我的代码...

    $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...我不明白!告诉我你又在用你的代码做什么?

    输出:

    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)
    

    【讨论】:

    • 请不要通过发布大量“答案”来进行“对话”。请改用编辑链接来改进您的答案。
    猜你喜欢
    • 2017-05-28
    • 2017-10-01
    • 2020-07-17
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 2015-01-19
    • 2017-08-05
    相关资源
    最近更新 更多