我想知道是否有办法在给定持续时间内获取两个时区之间给定日期范围的时区偏移量。
getTimezoneOffset(startDate,endDate,timezone1,timezone2){
...missing magic to go here...
}
应返回在给定持续时间内有效的时区偏移量。但是,如果偏移量发生变化,它应该返回其有效的日期范围。
所以我正在看这样的东西:
getTimezoneOFfset("march 9 12 am", "march 15 12 am", "UTC", "US/NEW_YORK")
返回值是这样的
timezoneoffset[0]["range"]=[march 9 12am to march 11 2 am]
timezoneoffset[0]["offset"]=5
timezoneoffset[1]["range"]=[march 9 2 am to march 15 12 am]
timezoneoffset[1]["offset"]=4
我只是不想为给定范围内的每个预定项目计算时区偏移量。正在寻找是否有某种方法可以直接查找偏移量(如果它要发生变化)。
我正在使用 PHP,但我将不胜感激任何语言的解决方案。
MySQL 解决方案也可以工作,因为它会在 MySQL 中进行更优化以执行此操作。
最佳答案
假设您有较新版本的 php (5.2.0+) DateTimeZone类是 PHP 核心的一部分,可让您使用 getOffset()
函数进行这些计算。它会让你传递一个 dateTime object并指定时区。如果你在两个日期都这样做,你应该能够计算出你想要捕获的所有部分。要使用来自 php.net 的示例:
// Create two timezone objects, one for Taipei (Taiwan) and one for
// Tokyo (Japan)
$dateTimeZoneTaipei = new DateTimeZone("Asia/Taipei");
$dateTimeZoneJapan = new DateTimeZone("Asia/Tokyo");
// Create two DateTime objects that will contain the same Unix timestamp, but
// have different timezones attached to them.
$dateTimeTaipei = new DateTime(mktime([the date in question]), $dateTimeZoneTaipei);
$dateTimeJapan = new DateTime(mktime([the date in question]), $dateTimeZoneJapan);
// Calculate the GMT offset for the date/time contained in the $dateTimeTaipei
// object, but using the timezone rules as defined for Tokyo
// ($dateTimeZoneJapan).
$timeOffset = $dateTimeZoneJapan->getOffset($dateTimeTaipei);
// Should show int(32400) (for dates after Sat Sep 8 01:00:00 1951 JST).
print("Number of seconds Japan is ahead of GMT at the specific time: ");
var_dump($timeOffset);
print("<br />Number of seconds Taipei is ahead of GMT at the specific time: ");
var_dump($dateTimeZoneTaipei->getOffset($dateTimeTaipei));
print("<br />Number of seconds Japan is ahead of Taipei at the specific time: ");
var_dump($dateTimeZoneJapan->getOffset($dateTimeTaipei)
-$dateTimeZoneTaipei->getOffset($dateTimeTaipei));
关于php - 获取给定持续时间内两个时区之间的时区偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9162697/