2015-05-12 80 views
0

我想爲不同的棒球隊輸入比賽時間。我想要使​​用下面的foreach循環。 PHP會將所有循環的時區設置爲America/New_York如何在foreach循環中設置不同的時區?

$BaseballTeams = array('America/New_York' => 'Maryville College', 'America/Chicago' => 'LeMoyne-Owen College', 'America/Denver' => 'Utah State', 'Pacific/Honolulu' => 'Hawaii Tech'); 

foreach ($BaseballTeams as $Key => $Value){ 

    date_default_timezone_set($Key); //Set the time zone for this team. 

    //Make a time stamp for that time zone 
    $TimeStamp = mktime($Hour,$Minute,$Second,$Month,$Day,$Year); 

    //Make a time stamp for that time zone 
    $MySQLi -> query("UPDATE Games SET GameDate = $TimeStamp WHERE TeamName = $Value"); 
} 
+0

而............ – AbraCadaver

+0

夏威夷科技的時間戳記輸入紐約時間。我嘗試使用偏移功能,但我無法讓它們正常工作。 $ Offset = new DateTime('2015-5-21',new DateTimeZone($ HomeTeamTimeZone)); echo $ Offset-> getOffset(); – Harvey

+1

此外,它看起來像你每次通過循環更新相同的記錄。 – AbraCadaver

回答

0

我找到了解決方案。找到UTC的偏移量。偏移量是一個對象。您必須將其分配給偏移量var。然後它變成一個整數。然後使用gmmktime減去偏移量來製作遊戲時間。

事情很簡單。但我一直在爲此工作數天和數天。

// -------------------------------------------------- 
    // ### Find the offset between utc and the home team time zone. ### 
    // -------------------------------------------------- 
    $Offset = new DateTime($Year.'-'.$Month.'-'.$Day, new DateTimeZone($HomeTeamTimeZone)); 
    // -------------------------------------------------- 
    // ### Offset is a type object. Assigning to offset var turns it into an integer. ### 
    // -------------------------------------------------- 
    $Offset = $Offset->getOffset(); 
    // -------------------------------------------------- 
    // ### Use greenwich mean time for the timestamp less the offset (which is in seconds). ### 
    // -------------------------------------------------- 
    $GameTime = gmmktime($Hour, $Minute, 1, $Month, $Day, $Year)-$Offset;