Advertisement
hexasoft

Display Visitor's Local Time Using PHP and BIN

May 16th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.18 KB | None | 0 0
  1. /****************************************************************************************************************/
  2. /* Description: This snippet is provide simple way to help user display Visitor's Local Time.                   */
  3. /*              suggestions for different city locations across the country.                                    */
  4. /*              There are 1 steps in this snippet. For information, please visit IP2Location tutorial page at:  */
  5. /* https://www.ip2location.com/tutorials/display-visitors-local-time-using-php-and-bin-database                 */
  6. /****************************************************************************************************************/
  7. /* You can obtain free IP2Location Lite database for bin at https://lite.ip2location.com/                       */
  8. /* You can obtain free IP2location PHP class module data at https://www.ip2location.com/developers/php         */
  9.  
  10. <?php
  11.     error_reporting(E_ALL);
  12.     ini_set('display_errors', 1);
  13.     set_time_limit(0);
  14.  
  15.     require_once('IP2Location.php');
  16.  
  17.     // Standard lookup with no cache
  18.     $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);
  19.  
  20.  
  21.     /*
  22.        Cache whole database into system memory and share among other scripts & websites
  23.        WARNING: Please make sure your system have sufficient RAM to enable this feature
  24.     */
  25.     //$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);
  26.  
  27.     /*
  28.        Cache the database into memory to accelerate lookup speed
  29.        WARNING: Please make sure your system have sufficient RAM to enable this feature
  30.     */
  31.     //$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);
  32.  
  33.     $ip = $_SERVER['REMOTE_ADDR'];
  34.  
  35.     $utc_time = gmdate("H:i:s");
  36.     $utc_h = explode(':', $utc_time);
  37.      
  38.     $time_zone = $loc->lookup($ip, \IP2Location\Database::TIME_ZONE);
  39.     $country_code = $loc->lookup($ip, \IP2Location\Database::COUNTRY_CODE);
  40.      
  41.     if (strcmp($time_zone, "-") == 0) {
  42.         $localdate = $utc_time;
  43.     }
  44.     else {
  45.         $time = explode(':', $time_zone);
  46.  
  47.         if ($utc_h[0] + $time[0] < 0) {
  48.             $hour = $utc_h[0] + 24 + $time[0];
  49.         }
  50.         elseif ($utc_h[0] + $time[0] >= 24) {
  51.             $hour = $utc_h[0] + $time[0] - 24;
  52.         }
  53.         else {
  54.             $hour = $utc_h[0] + $time[0];
  55.         }
  56.  
  57.         $localdate = $hour . gmdate(":i:s");
  58.     }
  59.  
  60.     date_default_timezone_set("Asia/Kuala_Lumpur");
  61.     $date = date_create($localdate);
  62.     echo 'Country Code: ' . $country_code . '<br />';
  63.     echo 'Local Time: ' . date_format($date, "H:i:s") . ' (' . $time_zone . '). <br />';
  64.     echo 'Server Time: ' . strftime("%H:%M:%S (%z)") . '<br />';
  65.     echo 'UTC Time: ' . gmdate("H:i:s (e)") . '<br />';
  66. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement