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('now', $dateTimeZone); $timeOffset = $dateTimeZone->getOffset($dateTime); $timeOffsetStr = output_offset($timeOffset); echo "$timeZone\t$timeOffsetStr\n"; } function output_offset($offset) { $pre = $offset < 0 ? '-' : '+'; if ($offset < 0) $offset = -$offset; $hour = (int)($offset / 3600); $minute = (int)($offset / 60) % 60; return $pre . sprintf('%02d:%02d', $hour, $minute); }
2. JS获取
可以用JS获取,方法如下:
注意,getTimezoneOffset()函数以分钟为单位,显示与格林尼治时间相差的数值,所以需要除以60.
<script type="text/javascript"> var d = new Date(); document.write(d.getTimezoneOffset()/60); </script>