2012-02-06 76 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

相關問題