Hey people,

Is there an easy way to get a users timezone or location without them having to say where they live or what time zone they are in?

I just want to use it so I can monitor what time people are logging on to my site.

Many Thanks!

    Javascript might be the way to go

    Blu

      thanks for the reply.

      is there any way of doing it using PHP?

      if Javascripts the only way then do you know the functions that would be needed?

        Well, I don't think that user time zone is part of any of the PHP global arrays in the way that ip address and user browser information is. PHP's time and date functions work with the server, but of course you know what time zone that is in.

        I don't know much about javascript other than it is client side, but have used it to get the time to display on users pages when I didn't want to use server time as my server is in Germany which is 1 hour ahead of the UK. I seem to remember with javascript you can use now.getHours() now.getMinutes() and now.getSeconds() to determine the local time which you could then compare with the PHP server time and detrmine the offset.

        I don't know if javascript has a way to determine the time offset from GMT.

        Blu

          thanks, ill have to look into more javascript and then hopefully I can just log the users actual time.

          my servers in usa so its about 6hrs behind uk time, which isn't really much of a problem, its just i wanted to know the users actual time so I could see if they log onto my site during the night, day, evening etc.

          thanks for the help.
          ill mark this thread resolved when I hopefully create the code and share it.

            There are databases out there that will supply an educated guess as to the location of any given IP address, though the little bit that I've looked into it would seem to indicate that the most reliable ones require a paid subscription (as the data is constantly evolving). I haven't looked into this for awhile, so you might want to search on something like "location by IP address" to see if you can turn up anything promising along that line (and maybe open source?).

            Other than that, you could certainly use JavaScript, perhaps along with an AJAX interface to PHP on your server. Just be sure to account for the fact that not everyone enables JavaScript on their browser, so you might want some sort of alternative in a <noscript> section of your page if this functionality is important for your page.

              I think you would do something like this:
              var hours = now.getHours
              var mins = now.getMinutes

              and then within a form .....
              <input type=hidden name=time value=document.write(hours + mins)>
              (This is what the code might look like and has no punctuation)

              Blu

                The IP-to-name databases are only reliable for determining the country of origin (even then, not 100%). Therefore, this approach won't work for countries containing more than one time zone.

                The client machine's clock is only as accurate as the operating system / user has set it - it could be wildly wrong (in which case I suppose it's safe to ignore it).

                Ultimately you want to find out the name of the PHP time zone the user wants to use, so you can set it in date_default_timezone_set

                Mark

                  I think I may go for the javascript option - i know this is based on the OS time, but I think most people do set their clocks correctly.

                  Javascript is: (found on some site :S)

                   <SCRIPT language="JavaScript">
                   <!--
                   var time = new Date()
                   var hour = time.getHours()
                   var min = time.getMinutes()
                   var sec = time.getSeconds()
                   document.write(hour + ":" + min + ":" + sec)
                   //-->
                   </SCRIPT>
                  

                  I think I will also log the actual database time just as a reference.

                    Its a pity you can't do something like this with javascript:

                    $usertime='document.write(hour + ":" + min + ":" + sec)';

                    and then insert usertime into the db via PHP

                    Blu

                    Actually it might even be possible, my javascript aint very good !!!

                      No, it isn't possible this way, as document.write writes the text to the browser on the client's computer. However you can use AJAX to send hour, min and sec to your server in GET or POST request. AJAX works in client's browser in JavaScript and can send request to your server with the information you need.

                        Write a Reply...