Advertisement
hexasoft

Display Sunrise and Sunset Time for IPV4

May 16th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.10 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 IPv4 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 = Dot2LongIP($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 ip2location_db11 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.  
  54.     //Displaying output
  55.     echo 'Country Code: ' . $country_code . '<br />';
  56.     echo 'Sunset Time: ' . $sunset . '<br />';
  57.     echo 'Sunrise Time: ' . $sunrise . '<br />';
  58.     echo 'Latitude: ' . $latitude . '<br />';
  59.     echo 'Longitude: ' . $longitude . '<br />';
  60.     echo $time_zone;
  61.  
  62.     // Function to convert IP address (xxx.xxx.xxx.xxx) to IP number (0 to 256^4-1)
  63.     function Dot2LongIP ($IPaddr) {
  64.         if ($IPaddr == "")
  65.         {
  66.         return 0;
  67.         }
  68.         else {
  69.         $ips = explode(".", $IPaddr);
  70.         return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
  71.         }
  72.     }
  73. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement