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

后端 未结 6 1272
逝去的感伤
逝去的感伤 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:23

    Id recommend using server time. In example, the time() function. If you base EVERYTHING off this value, then your system will be safe from any time specific operations you need.

    Everytime a user requests a page that has or uses time contraints, make sure you send the server time.

    Create a Javascript module that, on page load, checks if the server has sent the time, and if so reads the value, and converts it to the JS local time, using a JS fetched or calculated time zone. Then you could save it as a JS global or slam it as a static var in an object or something.

提交回复
热议问题