How to Get Time Zone Through Ip Address in PHP

How to get the Timezone in php based on client IP

Here i found the solution:

$user_ip = getenv('REMOTE_ADDR'); 
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
$country = $geo["geoplugin_countryName"];
$city = $geo["geoplugin_city"];

How to get timezone by IP

You can use GeoIP based services http://www.php.net/manual/en/book.geoip.php

Or

You can use online services that gives timezone of the IP that you supply in request

i.e.

$tags = get_meta_tags('http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=94.55.180.139'); 
echo $tags['timezone'];

Getting timezone via IP

ip-api.com responds with data based on the IP from which the request came. If you're in the Philippines and are accessing the website, you'll see information about yourself. If your server is in America and is requesting from the site, it'll see information about itself.

If you want your server to retrieve information about the client that's visiting it, you'll have to pass along the visiting client's IP. According to the documentation, you append it to the URL:

file_get_contents('http://ip-api.com/json/' . $_SERVER['REMOTE_ADDR'])

Timezone detection from Clients' IP Address

There are various ways of guessing the timezone. Using a service like in your question is one, although that code looks to me like it gets the timezone of the local computer, but I could be wrong. In any case, it doesn't seem reliable.

Another way is the jstimezonedetect javascript library.

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

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

Example using a callback:

  • use a session in PHP (session_start, or create your own session handler)
  • 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

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.

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

Keep the holding page blank and no-one will notice it - just looks like the page is loading.

PHP - Get Time Zone By Country Name

You can use https://freegeoip.net/json/[IP address] to get all the information you want.



Related Topics



Leave a reply



Submit