hexasoft icon

Display Visitor's Local Time Using PHP and BIN

hexasoft | PRO | 05/16/18 08:14:27 AM UTC | 0 ⭐ | 244 👁️ | Never ⏰ | []
PHP |

3.18 KB

|

None

|

0 👍

/

0 👎

/****************************************************************************************************************/
/* Description: This snippet is provide simple way to help user display Visitor's Local Time.                   */
/*              suggestions for different city locations across the country.                                    */
/*              There are 1 steps in this snippet. For information, please visit IP2Location tutorial page at:  */
/* https://www.ip2location.com/tutorials/display-visitors-local-time-using-php-and-bin-database                 */
/****************************************************************************************************************/
/* You can obtain free IP2Location Lite database for bin at https://lite.ip2location.com/                       */
/* You can obtain free IP2location PHP class module data at https://www.ip2location.com/developers/php         */
 
<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    set_time_limit(0);
 
    require_once('IP2Location.php');
 
    // Standard lookup with no cache
    $loc = new \IP2Location\Database('./databases/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-SAMPLE.BIN', \IP2Location\Database::FILE_IO);
 
 
    /*
       Cache whole database into system memory and share among other scripts & websites
       WARNING: Please make sure your system have sufficient RAM to enable this feature
    */
    //$loc = new \IP2Location\Database('./databases/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-SAMPLE.BIN', \IP2Location\Database::SHARED_MEMORY);
 
    /*
       Cache the database into memory to accelerate lookup speed
       WARNING: Please make sure your system have sufficient RAM to enable this feature
    */
    //$loc = new \IP2Location\Database(ROOT . './databases/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-SAMPLE.BIN', \IP2Location\Database::MEMORY_CACHE);
 
    $ip = $_SERVER['REMOTE_ADDR'];
 
    $utc_time = gmdate("H:i:s");
    $utc_h = explode(':', $utc_time);
     
    $time_zone = $loc->lookup($ip, \IP2Location\Database::TIME_ZONE);
    $country_code = $loc->lookup($ip, \IP2Location\Database::COUNTRY_CODE);
     
    if (strcmp($time_zone, "-") == 0) {
        $localdate = $utc_time;
    }
    else {
        $time = explode(':', $time_zone);
 
        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");
    }
 
    date_default_timezone_set("Asia/Kuala_Lumpur");
    $date = date_create($localdate);
    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 />';
?>

Comments