PHP世界时间与BST更新


PHP World times with BST update

我试图在我的网站上显示一些世界时间。我正在寻找最精确的方式来更新时间/保持它们的准确性,同时加入BST 1小时跳跃。

到目前为止,我有

<?php
date_default_timezone_set('Europe/London');
$uk_time = date("g:i A");
$month = date("n");
if ($month < 3 || $month > 10)  //January, February, November and December are not BST months.
{
$us_time = date("g:i A", strtotime('-4 hours'));
$dub_time = date("g:i A", strtotime('+4 hours'));
$kong_time = date("g:i A", strtotime('+8 hours'));
}
elseif ($month > 3 || $month < 10)  //April to September are BST months.
{
$us_time = date("g:i A", strtotime('-5 hours'));
$dub_time = date("g:i A", strtotime('+3 hours'));
$kong_time = date("g:i A", strtotime('+7 hours'));
}
?>
<div class="time-left">
<ul>
  <li class="clock"><strong>New York:</strong> <?php echo "$us_time" ?></li>
  <li><strong>London:</strong> <?php echo "$uk_time" ?></li>
  <li><strong>Dubai:</strong> <?php echo "$dub_time" ?></li>
  <li><strong>Hong Kong:</strong> <?php echo "$kong_time" ?></li>
</ul>
</div>

我想知道是否有更好的方法

欢呼

Jeff

是的。使用DateTime对象(与DateTimeZone对象一起使用。您可以使用此列表中的时区实例化更多DateTimeZone对象。

示例:

<?php
    $london_tz    = new DateTimeZone("Europe/London");
    $hong_kong_tz = new DateTimeZone("Asia/Hong_Kong");
    $dubai_tz     = new DateTimeZone("Asia/Dubai");
    $london_time    = new DateTime("now", $london_tz);
    $hong_kong_time = new DateTime("now", $hong_kong_tz);
    $dubai_time     = new DateTime("now", $dubai_tz);
    echo $london_time->format("H:i:s Y-m-d") . PHP_EOL;
    echo $hong_kong_time->format("H:i:s Y-m-d") . PHP_EOL;
    echo $dubai_time->format("H:i:s Y-m-d") . PHP_EOL;

PHP的DateTime和DateTimeZone的伟大之处在于,它们为您带来了所有的魔力,您不需要考虑DST或任何其他奇怪的自定义因素,DateTime已经知道:)

<?php
$date = new DateTime(null, new DateTimeZone('Europe/London'));
$tz = $date->getTimezone();
echo $tz->format('Y-m-d H:i:s');
$date = new DateTime(null, new DateTimeZone('Asia/Hong_Kong'));
$tz = $date->getTimezone();
echo $tz->format('Y-m-d H:i:s');
?> 

等等。。。

以下是支持的时区列表:http://php.net/manual/en/timezones.php