Determine a user's timezone

前端 未结 25 2895
刺人心
刺人心 2020-11-21 07:12

Is there a standard way for a web server to be able to determine a user\'s timezone within a web page?

Perhaps from an HTTP header or part of the user-agent

25条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 07:33

    Using Unkwntech's approach, I wrote a function using jQuery and PHP. This is tested and does work!

    On the PHP page where you want to have the timezone as a variable, have this snippet of code somewhere near the top of the page:

    
    

    This will read the session variable "time", which we are now about to create.

    On the same page, in the , you need to first of all include jQuery:

    
    

    Also in the , below the jQuery, paste this:

    
    

    You may or may not have noticed, but you need to change the URL to your actual domain.

    One last thing. You are probably wondering what the heck timezone.php is. Well, it is simply this: (create a new file called timezone.php and point to it with the above URL)

    
    

    If this works correctly, it will first load the page, execute the JavaScript, and reload the page. You will then be able to read the $timezone variable and use it to your pleasure! It returns the current UTC/GMT time zone offset (GMT -7) or whatever timezone you are in.

提交回复
热议问题