how to make datetime class to work with DST and without DST in php?

那年仲夏 提交于 2019-12-08 05:00:51

问题


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:


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)


来源:https://stackoverflow.com/questions/5471448/how-to-make-datetime-class-to-work-with-dst-and-without-dst-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!