0

I have a website where I want to capture the visitors login (date/time) based on his country, so that I can show him, his last login detail when he logs in again into my website. I have tried it like this:

$date = new DateTime('now', new DateTimeZone('Australia/ACT'));<br>
$current_time=$date->format(Y-m-d'H:i:s');

I want to make it dynamically

Please help

Thanks

StBlade
  • 287
  • 6
  • 18
user7789076
  • 798
  • 2
  • 12
  • 25

3 Answers3

0

The client's timezone is only obtainable via client-side methods. This means PHP (the server) will never know the requesting client's TZ.

See see How to get client's timezone? and Getting the client's timezone in JavaScript.

Community
  • 1
  • 1
zamnuts
  • 9,492
  • 3
  • 39
  • 46
  • You can get the country and thus time zone in the server side. – Carlos Oct 11 '13 at 06:58
  • @jackflash (1) Many countries have multiple timezones: USA has 4, Russia has 10. With that, you'll have to have a region/city database for geoip lookup to really know the TZ, and even then they are not entirely accurate. (2) If the user is browsing via proxy, then the IP address won't reflect their geolocation, JavaScript is the only trustworthy source, albeit GeoIP is much more simple. (3) The GeoIP method also assumes Net_GeoIP is available/installed and that the MaxMind DATs are kept up-to-date. – zamnuts Oct 11 '13 at 07:01
  • You can also get the region with geoip. It will work in most cases and if someone is browsing through a proxy he should know the limitations this implies. The point is: you CAN get the user's time zone from the server side. – Carlos Oct 11 '13 at 07:06
  • @jackflash, you can assume the user's TZ from a reverse geoip lookup; all I'm saying is the *true* answer is via client-side – zamnuts Oct 11 '13 at 07:08
  • 1
    There is not a *true* answer actually, if users disable Javascrip there is no go with this. – Carlos Oct 11 '13 at 07:36
0

You can use PECL's geoip: http://pecl.php.net/package/geoip

Here is the documentation: http://www.php.net/manual/en/ref.geoip.php

That will allow you to locate your users according to their IPs (geoip_country_code_by_name() and geoip_region_by_name()). Also, it has a function that maps country codes and time zones (geoip_time_zone_by_country_and_region()).

Example:

function getTimezone($ip)
{
    return geoip_time_zone_by_country_and_region(
        geoip_country_code_by_name ($ip),
        geoip_region_by_name($ip)
    );
}
Carlos
  • 4,949
  • 2
  • 20
  • 37
0

First we get the client IP using

$ip     = $_SERVER['REMOTE_ADDR'];

then using goip we can get users country and timezone. Its easy.

Praveen D
  • 2,337
  • 2
  • 31
  • 43