Advertisement
hexasoft

Display Visitor's Local Time for IPV4

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