Automatically detect user's current local time with JavaScript or PHP

后端 未结 6 1273
逝去的感伤
逝去的感伤 2020-11-29 04:54

I often need to display information based on or influenced by a user\'s actual local time which differs across time zones. Is there a reliable way of getting a user

6条回答
  •  忘掉有多难
    2020-11-29 05:35

    With JavaScript, there is a way to get the users current timezone offset.

    new Date().getTimezoneOffset() * -1
    

    returns the offset in minutes from GMT. Do not store this value as it can change depending on Daylight Saving Time. If you need it on the server (php) then you'll need to capture it in send it.

    Unfortunately, the HTTP spec doesn't send the users timezone as a header, which is what php would need.

提交回复
热议问题