Advertisement
hexasoft

Display Sunrise and Sunset Time for IPV6

May 16th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.24 KB | None | 0 0
  1. /****************************************************************************************************************/
  2. /* Description: This snippet is provide simple way to help user display Visitor's Sunrise and Sunset 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-sunrise-sunset-time-using-php-and-mysql-database                */
  6. /****************************************************************************************************************/
  7. /* You can obtain free IP2Location LITE database for IPv6 at https://lite.ip2location.com/                      */
  8.  
  9. <?php
  10.     // MySQL configuration
  11.     $mysql_server = "mysql_server.com";
  12.     $mysql_user_name = "User ID";
  13.     $mysql_user_pass = "password";
  14.  
  15.     $ip = $_SERVER['REMOTE_ADDR'];
  16.     $ipno = Dot2LongIPv6($ip);
  17.          
  18.     // Connect to the database server
  19.     $link = mysqli_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");
  20.  
  21.     // Connect to the IP2Location database
  22.     mysqli_select_db($link,"ip2location") or die("Could not select database");
  23.  
  24.     // SQL query string to match the recordset that the IP number fall between the valid range
  25.     $query = "SELECT time_zone, country_code, latitude, longitude FROM db11lite WHERE $ipno <?= ip_to LIMIT 1";
  26.      
  27.     // Execute SQL query
  28.     $result = mysqli_query($link,$query) or die("IP2Location Query Failed");
  29.  
  30.     // Retrieve the recordset (only one)
  31.     $row = mysqli_fetch_assoc($result);
  32.      
  33.     // Retrieving data from database
  34.     $country_code = $row['country_code'];
  35.     $time_zone = $row['time_zone'];
  36.     $latitude = $row['latitude'];
  37.     $longitude = $row['longitude'];
  38.  
  39.     // Free recordset and close database connection
  40.     mysqli_free_result($result);
  41.     mysqli_close($link);
  42.      
  43.     // Setting Sun's zenith value
  44.     $zenith = 90+50/60;
  45.     $offset = explode(':', $time_zone);
  46.  
  47.     date_default_timezone_set("UTC");
  48.     $date = date_create("UTC");
  49.  
  50.     $sunset = date_sunset(date_timestamp_get($date), SUNFUNCS_RET_STRING, $latitude, $longitude, $zenith, $offset[0]);
  51.     $sunrise = date_sunrise(date_timestamp_get($date), SUNFUNCS_RET_STRING, $latitude, $longitude, $zenith, $offset[0]);
  52.  
  53.     //Displaying output
  54.     echo 'Country Code: ' . $country_code . '<br />';
  55.     echo 'Sunset Time: ' . $sunset . '<br />';
  56.     echo 'Sunrise Time: ' . $sunrise . '<br />';
  57.     echo 'Latitude: ' . $latitude . '<br />';
  58.     echo 'Longitude: ' . $longitude . '<br />';
  59.     echo $time_zone;
  60.  
  61.     // Function to convert IP address to IP number (IPv6)
  62.     function Dot2LongIPv6 ($IPaddr) {
  63.         $int = inet_pton($IPaddr);
  64.         $bits = 15;
  65.         $ipv6long = 0;
  66.         while($bits >= 0){
  67.             $bin = sprintf("%08b", (ord($int[$bits])));
  68.             if($ipv6long){
  69.                 $ipv6long = $bin . $ipv6long;
  70.             }
  71.             else{
  72.                 $ipv6long = $bin;
  73.             }
  74.             $bits--;
  75.         }
  76.         $ipv6long = gmp_strval(gmp_init($ipv6long, 2), 10);
  77.         return $ipv6long;
  78.     }
  79. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement