Google Ads

Display Visitor’s Local Time Using PHP and BIN Database PHP Script


Display Visitor’s Local Time Using PHP and BIN Database PHP Script

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/

 

 
 
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', 1);
  4. set_time_limit(0);
  5. require_once('IP2Location.php');
  6. $loc = new IP2Location('databases/DB24-SAMPLE.BIN', IP2Location::FILE_IO);
  7. //$loc = new IP2Location('databases/IP-COUNTRY-SAMPLE.BIN', IP2Location::SHARED_MEMORY);
  8. //$loc = new IP2Location(ROOT . 'databases/IP-COUNTRY-SAMPLE.BIN', IP2Location::MEMORY_CACHE);
  9. $ip = $_SERVER['REMOTE_ADDR'];
  10. $utc_time = gmdate("H:i:s");
  11. $utc_h = explode(':', $utc_time);
  12. $time_zone = $loc->lookup($ip, IP2Location::TIME_ZONE);
  13. $country_code = $loc->lookup($ip, IP2Location::COUNTRY_CODE);
  14. if (strcmp($time_zone, "-") == 0) {
  15.       $localdate = $utc_time;
  16. }
  17. else {
  18.       $time = explode(':', $time_zone);
  19.       if ($utc_h[0] + $time[0] < 0) {
  20.             $hour = $utc_h[0] + 24 + $time[0];
  21.       }
  22.       elseif ($utc_h[0] + $time[0] >= 24) {
  23.             $hour = $utc_h[0] + $time[0] - 24;
  24.       }
  25.       else {
  26.             $hour = $utc_h[0] + $time[0];
  27.       }
  28.       $localdate = $hour . gmdate(":i:s");      
  29. }
  30. date_default_timezone_set("Asia/Kuala_Lumpur");
  31. $date = date_create($localdate);
  32. echo 'Country Code: ' . $country_code . '<br>';
  33. echo 'Local Time: ' . date_format($date, "H:i:s") . ' (' . $time_zone . '). <br>';
  34. echo 'Server Time: ' . strftime("%H:%M:%S (%z)") . '<br>';
  35. echo 'UTC Time: ' . gmdate("H:i:s (e)") . '<br>';
  36. ?>

 

 

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

"