Determining Your Visitor's Time Zone
You can capture your visitor's time and time zone for your own information. And you can let your visitor know the current time on your business office clocks.
But why would you want to?
-
You can determine what time of day (their time) your visitors are most active (for your feedback forms, your page statistics, information for your marketing strategies, etc.) Do they visit your page soon after they get to the office? Maybe most do so on their lunch break? Or, possibly, your site is visited most often after their kids are put to bed?
-
If your visitors email you in the middle of your sleep period, they may understand not receiving an answer right away.
-
Your visitors can determine your office hours translated to their own time zone.
-
You can provide your visitor with that extra tidbit of information which you, yourself, have probably wanted to know when viewing someone's web site: "Gee, I wonder what time it is where they're from."
The idea of time zones is a wonderful solution for coordinating time worldwide. However, when governments got their paws on it, it became confusing to the logical brain.
Explanations of time zones can be found at http://tycho.usno.navy.mil/tzones.html and http://times.clari.net.au/Australia.html
You can look up the Universal and local time for most regions of the world at http://www.cstv.to.cnr.it/toi/uk/timezone.html [Link no longer valid. Google may be able to find others.]
The JavaScript code in this article will put order into the confusions:
-
Your visitors' time is the correct time. For them. Which probably is different than yours. And your web site could be on a server in a different time zone altogether. And, any could be on either side of the International Date Line.
-
(It took over a century for all countries to adopt the Gregorian Calendar. Thus, it may be a bit much to expect every political power to agree on what time it is.)
The answer is to base these time calculations on the clock and time zone information contained within the visitor's own computer.
You know what time it is for you. Your visitors know what time it is for them. Now, both of you can know both.
There are two separate, independent, sets of code presented in this article. (1) How to gather your visitor's time zone information. (2) How to present your visitor with your own local time.
Note: This article contains both JavaScript and HTML code. If you are reading this on an HTML-enabled email program, you may need to "view source" for all the code to be visible. As an alternative, you may view this article in the archives (which will present the code correctly in your browser). The archives are at /library/
(1) How to gather your visitor's time zone information
Use your existing web site forms -- feedback, subscription, recommend, etc. -- and place a hidden form field into them. The hidden field gathers the information and transmits it to your CGI program whenever the submit button is clicked. The hidden field's name can be anything your program can use.
The following works just fine with MasterFeedback. See the documentation of your CGI programs to determine whether they will accept additional hidden form fields and what the fields should be called; and modify the name="..." portion as needed.
Insert the code anywhere between your <form... and </form> tags:
The above is 15 lines of code. JavaScript is line break sensitive. If your email program shows more than 15 lines of code, then please obtain your code from this issue's archive, linked from /library/
If your visitor's browser is JavaScript enabled and can handle the Date() function, then the code will provide a hidden field containing the number of hours your visitor's location is from Greenwich Mean Time. Otherwise, the hidden field contains a descriptive message.
The number of hours contained in the hidden field is either positive or negative. If positive, it represents the number of hours earlier than Greenwich Mean Time. If negative, later. (Note: This is opposite of most time zone maps; an idiosyncrasy of JavaScript.)
http://swissinfo.net/cgi/worldtime/ and http://www.worldtimezone.com/ provide time zone maps from which you can view your visitor's geographical time zone location.
FYI, MasterFeedback is free and can be obtained from /software/feedback
(2) How to present your visitor with your own local time
This requires placing JavaScript in two places on your HTML page: (i) between your <head... and </head> tags, and (ii) between your <body... and </body> tags.
It also requires you to customize one line. To do that, you need to know how many minutes you are from Greenwich Mean Time. The number could be either positive or negative. If your time is earlier than Greenwich Mean Time then the number is positive. If your time is later, the number is negative. (Again, this is opposite of most time zone maps; an idiosyncrasy of JavaScript.)
Get the number quick and easy! Place the following 15 lines of code into a web page and load the page into your browser:
Either way, you will have obtained the number you need to customize the one line.
The JavaScript code presented below will display timezone information on your page.
Here is the first part, which belongs between your <head... and </head> tags (the third line will need to be customized):
On the third line of the above code, you see the number 300. That is the number which must be changed to reflect the number of minutes you are from Greenwich Mean Time.
The next 18 lines of code belong between your <body... and </body> tags:
Place the above code wherever you want to display the data.
Will Bontrager
©1999 Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.