开发者

Determining the user's time and date in PHP

I want to be able to get the time of the person using the PHP script.

I don't know what time zone they will be in, etc.

Is there not a way of just saying 'get the time of the user开发者_JAVA技巧' rather than setting timezones?


No. There are only three ways to get the time zone of the user:

  1. Ask the user

  2. Use JavaScript to check the time client-side and send it to your server with an AJAX request, a cookie, etc. -- only works if JS is enabled and doesn't work for the first request

  3. Use an IP-to-location database to locate the user and determine their time zone from the location -- more than 5% of the time with any commercial database, your location at the city level will be wrong


No, there is not. The only thing you get to know about your users "for free" is what can be communicated via the HTTP protocol, and that doesn't include "user timezone."

You could use Javascript to get the time from the browser, but that's not guaranteed to be accurate.

You could try to geolocate the user, but that's even less accurate.

The user will know best himself, ask him.


the person using the php script.

That's the webserver. If you mean the person requesting the PHP script, you don't know their timezone unless they're nice enough to send it to you. Firefox does not appear to, although some may.

The answer here is to use Javascript to get the local time and either do what needs doing client-side, or send it to the server somehow - but that's a different question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜