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

How to set different time zones in foreach loop?

我想输入不同棒球队的比赛时间。我想使用如下所示的 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");
}

我找到了解决方案。查找与 UTC 的偏移量。偏移量是一个对象。您必须将其分配给偏移量变量。然后它变成一个整数。然后使用 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;