1

我想根据国家/地区显示 GMT 时间。如果假设,来自印度的某个用户注册到我的应用程序中,我应该将 GMT 时间作为 GMT +5 附加到我的数据库中。如果假设,来自法国的其他用户注册到我的应用程序中,我应该将 GMT 时间作为 GMT +1 附加到我的数据库中。

4

2 回答 2

1

如果您想在 PHP 中执行此操作,则需要为此进行地理定位。有两种方式:自制方式,或API方式。根据您想要的分辨率,情况可能会有所不同。请记住,这不会神奇地检测代理。权衡是在效率和内务管理之间。

家酿

拿起一个地理定位数据库并查询它的 IP 子集,这将为您提供国家/地区。保留国家/地区的时区列表。这很痛苦,这就是为什么我不打算为此提供代码。

API

这是微不足道的:

<?php 
  $geoloc = file_get_contents("http://api.ipinfodb.com/v3/ip-city/?format=json&key=<API KEY>&ip=".$_SERVER['REMOTE_ADDR']);
  if ($geoloc && ($r = json_decode($geoloc)) !== false) {
    // Your timezone is in $r->timeZone in the format "+/-i:ii"
  }
?>

You'll need an API key for ipinfodb, which you can get at this address: http://ipinfodb.com/register.php

于 2013-04-22T10:54:53.987 回答
0

有时,没有server-based/PHP获取本地时间的方法。

您必须通过Javascript. 谷歌bitbucket timezone detect并使用它来设置一个local_timezonecookie,您可以从 PHP 读取并通过date_default_timezone_set()

//get the client ip and assign it to variable
$IP=$_SERVER['REMOTE_ADDR'];
//if you using localhost you can check using follow variable
//$IP = '202.71.158.30';
//pass the ip as a parameter for follow URL it will return the country
$country = file_get_contents('http://api.hostip.info/country.php?ip='.$IP);
//show the country
echo $country

此外,这里是帮助您获取 GTM 时区的链接......

如何在 PHP 中获得格林威治标准时间?

您需要实现自己的逻辑,例如,获取客户的时间并将其从 GTM-0 中扣除,您将得到的将是 GTM(小时).. 希望您能得到我想说的..

希望能帮助到你。

于 2013-04-22T10:49:43.683 回答