如何使datetime类工作在DST和没有DST在php? [英] how to make datetime class to work with DST and without DST in php?

查看:104
本文介绍了如何使datetime类工作在DST和没有DST在php?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了我使用的DateTime类的代码,它也将以下国家/地区的时间转换为GMT格式。
但是我想要的功能,我需要的DateTime类应该工作在复选框选择。
当用户检查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.......

推荐答案

尝试这段代码,让我知道它可以工作吗?

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

其他参数 $ considerDST 是布尔值,所以传递true(或跳过此参数;它的默认值)如果你想要DST,或者如果没有,则为false。

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

如果要将时间转换为UTC,您需要通过相同的时间输入函数的参数。

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

您可以这样做!

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

并期待与UTC相同的UTC UTC ...当贝尔格莱德12:52时,它的纽约时间06:52,所以...

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

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

has same UTC time 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>";

同一天UTC 10:00 ...我不明白!告诉我你再次使用代码怎么办?

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)

这篇关于如何使datetime类工作在DST和没有DST在php?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆