I want to show GMT Timebased upon country. If suppose, some user from india, register into my application i should append GMT time as GMT +5 into my database. If suppose, some other user from france, register into my application i should append GMT time as GMT +1 into my database.

有帮助吗?

解决方案

If you want to do it in PHP, you'll need geolocation for this. There are two ways: the homebrew way, or the API way. Depending on the resolution you would like, things might differ. Bear in mind that this will not magically detect proxies. The trade-offs are between efficiency and housekeeping.

Homebrew

Pick up a geolocation DB and query it for the IP subset, which will give you the country. Keep a list of timezones for countries. It's a pain, which is why I'm not going to give code for it.

API

This one is trivial:

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

其他提示

Sometimes, there's not a server-based/PHP method for getting local time.

You have to get it from the client via Javascript. Google bitbucket timezone detect and use it to set a local_timezone cookie that you can read from PHP and set via 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

Further more, here is the link which will help you to get the GTM timezone...

How do I get Greenwich Mean Time in PHP?

You need to implement your own logic, for example, get the client's time and deduct it from GTM-0, what you will get will be the GTM (hours).. Hope you get what I want to say..

Hope it helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top