How to get client timezone in utc format like (UTC

2019-08-19 04:42发布

问题:

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