Question

To get UTC offset in php I could use date("Z") but as I think this will return the UTC offset relative to timezone of the server not relative to timezome of the user's computer, which I want to get. And when I searched I found that getting UTC offset relative to timezone of the user's computer can be done through the javascript getTimezoneOffset() function which returns the time difference between UTC time and local time, in minutes. And I have successfully used this javascript function in my php script. Now my question is: Is the javascritp getTimezoneOffset() function is always (or in most cases) reliable to return the accurate UTC offset relative to the user's computer?

Was it helpful?

Solution

This will depend on the user's date/time settings. It will give you the offset based on what the browser sees the timezone is set on the user's computer.

But note that when you are using javascript, it is running on the client-side, i.e. on the browser, and not on the server-side. So you cannot use it in PHP, unless:

  • You store it in a cookie (via javascript) the first time a user visits your website.
  • You make an AJAX call to store a PHP code to store it in the session.
  • If you want to display a time on the user's computer, you send the time difference between the date you want to show and now(), and then you add that to the user's getTime() or you display the difference (i.e. 7 hours ago, 2 minutes ago, etc.).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top