Home >php教程 >PHP源码 >PHP 输出的各个时区对应的时差表

PHP 输出的各个时区对应的时差表

PHP中文网
PHP中文网Original
2016-05-26 08:20:091293browse

php代码

<?php
$timezone_identifiers = DateTimeZone::listIdentifiers();
for ($i=0; $i < count($timezone_identifiers); $i++) {
        $timeZone = $timezone_identifiers[$i];
        $dateTimeZone = new DateTimeZone($timeZone);
        $dateTime = new DateTime(&#39;now&#39;, $dateTimeZone);
        $timeOffset = $dateTimeZone->getOffset($dateTime);
        $timeOffsetStr = output_offset($timeOffset);
        echo "$timeZone\t$timeOffsetStr\n";
}
function output_offset($offset) {
        $pre = $offset < 0 ? &#39;-&#39; : &#39;+&#39;;
        if ($offset < 0) $offset = -$offset;
        $hour = (int)($offset / 3600);
        $minute = (int)($offset / 60) % 60;
        return $pre . sprintf(&#39;%02d:%02d&#39;, $hour, $minute);
}

2. JS获取

可以用JS获取,方法如下:
注意,getTimezoneOffset()函数以分钟为单位,显示与格林尼治时间相差的数值,所以需要除以60.

<script type="text/javascript">
    var d = new Date();
    document.write(d.getTimezoneOffset()/60);
</script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn