2012-02-06 150 views
5

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

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中更好地完成此操作。

回答

3

假设您有较新版本的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)); 
+0

我知道得到抵消..但得到抵消刚插上U的为..the事情是我有一个时间表项目的时间表不同的时间安排在白天给定的时间偏移.. – user1192612 2012-02-06 21:31:59

+0

所以,如果我得到的请求获得2012年3月10日晚上9点汤姆·3月11日上午9点偏移要为THT时间..我不wnat计算偏移量每次..和这仅仅是美国..different国家都会有不同的变化之间的日程事项天偏移更改,..我试图找到两个时区之间的一套持续时间不一定UTC的问题..问题是我donno,如果我计算基于开始时间的偏移量,它将始终是相同的..我觉得PHP得到抵消ggivesü给定的时间..我希望我所做的偏移自己清楚 – user1192612 2012-02-06 21:39:48

+0

底线是我必须承担抵消在一天可能会发生变化说,12年3月11日上午牛逼Ømrach 11下午12点(偏移凌晨2点不同)..我试图掩盖这个角落情况下 – user1192612 2012-02-06 21:43:32

1

我认为PHP的DateTimeZone::getTransitions method可以帮到你。它有一个程序别名,timezone_transitions_get()

public array DateTimeZone::getTransitions (
    [ int $timestamp_begin [, int $timestamp_end ]] ) 

该方法返回给定时间范围内从一个时区偏移值到另一个时区的所有“转换”。

为了您的目的,您需要为每个时区创建DateTimeZone对象,请在日期范围内调用DateTimeZone::getTransitions以获取该时区的转换数组,然后合并并排序两个转换数组。这会给你相当于你所寻求的timezoneoffset[]阵列。

是这样的:

getTimezoneOffset(startDate,endDate,timezone1,timezone2){ 
    DT1 = DateTimeZone(timezone1); 
    transitions1 = DT1->getTransitions(startDate,endDate); 
    DT2 = DateTimeZone(timezone2); 
    transitions1 = DT2->getTransitions(startDate,endDate); 
    timezoneoffset[] = // merge and sort (transitions1, transitions2) 
} 

过渡阵列的格式没有很好的记载。该方法文档显示一些示例性输入:

Array 
(
... 
    [1] => Array 
     (
      [ts] => -1691964000 
      [time] => 1916-05-21T02:00:00+0000 
      [offset] => 3600 
      [isdst] => 1 
      [abbr] => BST 
     ) 

    [2] => Array 
     (
      [ts] => -1680472800 
      [time] => 1916-10-01T02:00:00+0000 
      [offset] => 0 
      [isdst] => 
      [abbr] => GMT 
     ) 
    ... 
) 

我推测:ts指划时代秒的PHP时间戳,通过time()作为返回,给即时在时间上在该记录中的偏移量改变为值。 time与格式化的字符串日期时间指向相同的时刻。 offset是从UTC开始的时区偏移量,从即时time/ts转发到下一个转换。 isdst如果偏移量是夏令时偏移量,则为1,否则为0。 abbr是时区的字符串缩写。如果任何人有关于此数据结构的可靠信息,将其添加到the documentation将是一种善意。

+0

谢谢吉姆..那正是我在找的东西..我经历了日期时间的对象,但这被忽略了。 – user1192612 2012-02-07 21:02:50