php通过时区代码/数字(+1、+2等)获取本地时间


php get local time by timezone code/digit (+1, +2 etc)

我正在努力寻找解决方案,有没有我可以使用php或js/jquery获取城市当地时间的函数/插件还是基于时区代码的状态?例如,假设罗马是时区2

非常感谢。

请查看gmstrftime():

<?php
    $offset = 2;
    $timestamp = time() + ( $offset * 60 * 60 );
    echo gmstrftime("%b %d %Y %H:%M:%S", $timestamp) . "'n";
?>

此函数根据GMT设置时间格式,因此您可以直接使用时区偏移。

PHP手册在这里-http://php.net/manual/en/function.gmstrftime.php

但一个更优雅的解决方案可能是使用DateTime::setTimezone():

<?php
// From PHP manual 
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "'n";
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "'n";
?>

我建议查看php中的DateTime类和支持的时区。