简体   繁体   English

如何根据访问者的IP地址显示本地化时间?

[英]How to display localized time, based on a visitor's Ip address?

I get the ip address of the user by function. 我按功能获取用户的IP地址。

e.g

$ip_address = getIP();

Now I want display current localized time and date based on a visitor's Ip address using PHP. 现在我希望使用PHP根据访问者的IP地址显示当前的本地化时间和日期。 if it is possible than guide me How? 如果有可能比指导我怎么样?

Waiting for the Help and Thanks In advance. 等待帮助并提前致谢。

As pointed out in comments - this has been asked and answered 正如评论中指出的那样 - 已经提出并回答了这个问题

But one answer not included is to use a Javascript callback, or store tiem offset in a cookie from Javascript (or adjust times on display). 但是不包括的一个答案是使用Javascript回调,或者在Javascript中存储cookie中的tiem偏移量(或调整显示的时间)。

Example using a callback: 使用回调的示例:

  • use a session in PHP (session_start, or create your own session handler) 在PHP中使用会话(session_start,或创建自己的会话处理程序)
  • either on the first page (where you don't have dates) or send a blank holding page that sets the session and use Javascript to redirect to the real page with appending the timezone difference 在第一页(您没有日期)或发送空白保持页面设置会话并使用Javascript重定向到真实页面并附加时区差异

Code: 码:

var d = new Date()
window.location.href = PageYouWant + '?offset=' + d.getTimezoneOffset();

(Vary according to your needs) (根据您的需要变化)

  • then on receiving the call back, remember the offset against the session. 然后在接收回叫时,记住会话的偏移量。 Then send a 303 redirect to the URL without the offset in the URL and you have the data you need. 然后发送303重定向到URL而不在URL中有偏移量,并且您拥有所需的数据。

You can do a similar thing with cookies (if you don't have the cookie, send a holding page that creates the cookie and calls you back). 你可以用cookies做类似的事情(如果你没有cookie,发送一个创建cookie的召开页面并给你回电话)。

Keep the holding page blank and no-one will notice it - just looks like the page is loading. 保持页面空白,没有人会注意到 - 只是看起来页面正在加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM