Home > php教程 > PHP源码 > body text

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

PHP中文网
Release: 2016-05-26 08:20:09
Original
1183 people have browsed it

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);
}
Copy after login

2. JS获取

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

<script type="text/javascript">
    var d = new Date();
    document.write(d.getTimezoneOffset()/60);
</script>
Copy after login
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!