How to get client timezone in utc format like (UTC+05:00) in php or javascript

ⅰ亾dé卋堺 提交于 2020-01-06 14:48:22

问题


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;

Result: Asia/Karachi

I want to get result in UTC format like (UTC+05:00). How can I achieve this?


回答1:


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'
);


来源:https://stackoverflow.com/questions/55903592/how-to-get-client-timezone-in-utc-format-like-utc0500-in-php-or-javascript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!