This sample code can be used to display visitor's local time based on their IP address using PHP programming languages and IP2Location BIN database. Free database is provided by IP2Location LITE at http://lite.ip2location.com/
<?php
require_once('IP2Location.php');
$loc = new IP2Location('databases/DB24-SAMPLE.BIN', IP2Location::FILE_IO);
//$loc = new IP2Location('databases/IP-COUNTRY-SAMPLE.BIN', IP2Location::SHARED_MEMORY);
//$loc = new IP2Location(ROOT . 'databases/IP-COUNTRY-SAMPLE.BIN', IP2Location::MEMORY_CACHE);
$ip = $_SERVER['REMOTE_ADDR'];
$time_zone = $loc->lookup($ip, IP2Location::TIME_ZONE);
$country_code = $loc->lookup($ip, IP2Location::COUNTRY_CODE);
if (strcmp($time_zone, "-") == 0) {
$localdate = $utc_time;
}
else {
if ($utc_h[0] + $time[0] < 0) {
$hour = $utc_h[0] + 24 + $time[0];
}
elseif ($utc_h[0] + $time[0] >= 24) {
$hour = $utc_h[0] + $time[0] - 24;
}
else {
$hour = $utc_h[0] + $time[0];
}
$localdate = $hour . gmdate(":i:s");
}
echo 'Country Code: ' . $country_code . '<br>';
echo 'Local Time: ' . date_format($date, "H:i:s") . ' (' . $time_zone . '). <br>';
echo 'Server Time: ' . strftime("%H:%M:%S (%z)") . '<br>';
echo 'UTC Time: ' . gmdate("H:i:s (e)") . '<br>';
?>
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.