PHP, how to set timezone?

2019-04-01 18:53发布

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..

标签: php timezone
2条回答
Explosion°爆炸
2楼-- · 2019-04-01 19:18

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.

查看更多
仙女界的扛把子
3楼-- · 2019-04-01 19:24

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>
查看更多
登录 后发表回答