如何在foreach循环中设置不同的时区?

时间:2015-05-12 17:51:01

标签: php date time

我想进入不同棒球队的比赛时间。我想要使​​用如下所示的foreach循环。 PHP会为所有循环将时区设置为America/New_York

$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");
}

1 个答案:

答案 0 :(得分: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;