2

I am using PHP and mysql.

Scenario:

The registration form only required 2 fields. Email and password, and then, first user registered from England, second user from China, etc..

Is there any way to set timezone automatically for different countries of users in PHP? I checked php.net, but no lucks..

roa3
  • 901
  • 8
  • 15
  • 27

2 Answers2

6

You could attempt to infer their country from their IP, and derive their timezone from that.

Alternatively, add something like this to the form and get Javascript to tell you the time zone offset used by the browser...

<script language="JavaScript">
<!-- 
    var d= new Date();
    document.write('<input type="hidden" name="timeZoneOffset" ');
    if (d) {
        document.write('value="' + d.getTimezoneOffset()/60 + '">');
    } else {
        document.write('value="n/a">');
    }
// -->
</script>
<noscript>
    <input type="hidden" name="timeZoneOffset" value="n/a">
</noscript>
Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • Thanks, I will try it out. Maybe I should start learning client-side script like javascript.. – roa3 Feb 13 '09 at 08:46
  • Note that this gives you the offset-from-UTC *now*; if the user lives in a region with DST that can of course change. – bobince Feb 13 '09 at 09:42
  • I tried this code and I got a positive offset, when it should have been negative. This code, however, gave me a far better result: http://bitbucket.org/pellepim/jstimezonedetect – YOMorales Jun 03 '11 at 19:23
1

No, that's not possible through php, since the browser doesn't always send that information. Last time we solved that problem by sending an ajax call and updating the timezone property of the session, since the javascript engine does have access to that browser property.

soulmerge
  • 73,842
  • 19
  • 118
  • 155