Advertisement
hexasoft

Display Visitor's Local Time for IPV6

May 16th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.64 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-mysql-database               */
  6. /****************************************************************************************************************/
  7. /* You can obtain free IP2Location DB11 LITE database for IPv6 at                                               */
  8. /*https://lite.ip2location.com/database/ip-country-region-city-latitude-longitude-zipcode-timezone              */
  9.  
  10. <?php
  11.  
  12.     // Replace this MYSQL server variables with actual configuration
  13.     $mysql_server = "mysql_server.com";
  14.     $mysql_user_name = "UserName";
  15.     $mysql_user_pass = "Password";
  16.      
  17.     // Retrieve visitor IP address from server variable REMOTE_ADDR
  18.     $ip = $_SERVER['REMOTE_ADDR'];
  19.  
  20.     // Convert IP address to IP number for querying database
  21.     $ipno = Dot2LongIPv6($ip);
  22.  
  23.     // Connect to the database server
  24.     $link = mysqli_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");
  25.  
  26.     // Connect to the IP2Location database
  27.     mysqli_select_db($link,"ip2location") or die("Could not select database");
  28.  
  29.     // SQL query string to match the recordset that the IP number fall between the valid range
  30.     $query = "SELECT time_zone, country_code FROM ip2location_db11 WHERE $ipno <= ip_to LIMIT 1";        
  31.      
  32.     // Execute SQL query
  33.     $result = mysqli_query($link,$query) or die("IP2Location Query Failed");
  34.  
  35.     // Retrieve the recordset (only one)
  36.     $row = mysqli_fetch_assoc($result);
  37.      
  38.     // Keep the time zone and country code as variables
  39.     $country_code = $row['country_code'];
  40.     $time_zone = $row['time_zone'];
  41.      
  42.     // Free recordset and close database connection
  43.     mysqli_free_result($result);
  44.     mysqli_close($link);
  45.  
  46.     $utc_time = gmdate("H:i:s");
  47.     $utc_h = explode(':', $utc_time);
  48.  
  49.     if (strcmp($time_zone, "-") == 0) {
  50.         $localdate = $utc_time;
  51.     }
  52.     else {
  53.         $time = explode(':', $time_zone);
  54.  
  55.         if ($utc_h[0] + $time[0] < 0) {
  56.             $hour = $utc_h[0] + 24 + $time[0];
  57.         }
  58.         elseif ($utc_h[0] + $time[0] >= 24) {
  59.             $hour = $utc_h[0] + $time[0] - 24;
  60.         }
  61.         else {
  62.             $hour = $utc_h[0] + $time[0];
  63.         }
  64.  
  65.         $localdate = $hour . gmdate(":i:s");
  66.     }
  67.  
  68.     date_default_timezone_set("Asia/Kuala_Lumpur");
  69.     $date = date_create($localdate);
  70.     echo 'Country Code: ' . $country_code . '<br />';
  71.     echo 'Local Time: ' . date_format($date, "H:i:s"). '<br />';
  72.     echo 'Server Time: ' . strftime("%H:%M:%S") . '<br />';
  73.     echo 'UTC Time: ' . gmdate("H:i:s") . '<br />';
  74.  
  75.     // Function to convert IP address to IP number (IPv6)
  76.     function Dot2LongIPv6 ($IPaddr) {
  77.         $int = inet_pton($IPaddr);
  78.         $bits = 15;
  79.         $ipv6long = 0;
  80.         while($bits >= 0){
  81.             $bin = sprintf("%08b", (ord($int[$bits])));
  82.             if($ipv6long){
  83.                 $ipv6long = $bin . $ipv6long;
  84.             }
  85.             else{
  86.                 $ipv6long = $bin;
  87.             }
  88.             $bits--;
  89.         }
  90.         $ipv6long = gmp_strval(gmp_init($ipv6long, 2), 10);
  91.         return $ipv6long;
  92.     }
  93. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement