在给定的持续时间内获得两个时区之间的时区偏移

use*_*612 6 php mysql timezone datetime datetimeoffset

我想知道是否有办法在给定的持续时间内获得两个时区之间给定日期范围的时区偏移量.

getTimezoneOffset(startDate,endDate,timezone1,timezone2){
    ...missing magic to go here...
}
Run Code Online (Sandbox Code Playgroud)

应该返回对给定持续时间有效的时区偏移量.但是,如果偏移量发生变化,则应返回其有效的日期范围.

所以我看的是这样的:

getTimezoneOFfset("march 9 12 am", "march 15 12 am", "UTC", "US/NEW_YORK")
Run Code Online (Sandbox Code Playgroud)

返回值这样的东西

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
Run Code Online (Sandbox Code Playgroud)

我只是不想为给定范围的每个计划项目计算时区偏移量.正在寻找是否有某种方法可以直接查找偏移量,如果它会发生变化的话.

我正在使用PHP,但任何语言的解决方案都将受到赞赏.

MySQL解决方案也将工作,因为它将在MySQL中进行更优化.

Ben*_*n D 4

假设您有较新版本的 php (5.2.0+),DateTimeZone类是 PHP 核心的一部分,并且允许您使用该getOffset()函数进行这些计算。它将让您传入一个dateTime 对象并指定一个时区。如果您对两个日期都执行此操作,您应该能够计算出您想要抓住的所有物品。使用 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));
Run Code Online (Sandbox Code Playgroud)