如何在 php 或 javascript 中以 utc 格式(如 (UTC+05:00))获取客户端时区 [英] How to get client timezone in utc format like (UTC+05:00) in php or javascript
本文介绍了如何在 php 或 javascript 中以 utc 格式(如 (UTC+05:00))获取客户端时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 php 或 javascript 中以 UTC 格式(如 (UTC+05:00))获取我的应用程序访问者的时区.
I want to get timezone of visitors of my application in UTC format like (UTC+05:00) in php or javascript.
我已经尝试过这个,但它并没有帮助我解决它返回的结果为亚洲/卡拉奇:
I have already tried this but it does not help me out it returns the result as Asia/Karachi:
$time ="<script>document.write(Intl.DateTimeFormat().resolvedOptions().timeZone);</script>";
echo $time;
结果:亚洲/卡拉奇
我想得到像 (UTC+05:00) 这样的 UTC 格式的结果.我怎样才能做到这一点?
I want to get result in UTC format like (UTC+05:00). How can I achieve this?
推荐答案
这是一个 javascript 解决方案:
Here is a javascript solution :
let date = new Date();
console.log('UTC' +
(-date.getTimezoneOffset() < 0 ? '-' : '+') +
(Math.abs(date.getTimezoneOffset() / 60) < 10 ? '0' : '') +
(Math.abs(date.getTimezoneOffset() / 60)) + ':00'
);
这篇关于如何在 php 或 javascript 中以 utc 格式(如 (UTC+05:00))获取客户端时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文