Advertisement
hexasoft

Display Sunrise and Sunset Time Using PHP and BIN

May 16th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.16 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-bin-database                  */
  6. /****************************************************************************************************************/
  7. /* You can obtain free IP2Location LITE database for bin at https://lite.ip2location.com/                       */
  8. /* You can obtain free IP2Location PHP class module data for at https://www.ip2location.com/developers/php      */
  9.  
  10. <?php
  11.     error_reporting(E_ALL);
  12.     ini_set('display_errors', 1);
  13.     set_time_limit(0);
  14.  
  15.     require_once('IP2Location.php');
  16.  
  17.     // Standard lookup with no cache
  18.     $loc = new \IP2Location\Database('./databases/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-SAMPLE.BIN', \IP2Location\Database::FILE_IO);
  19.  
  20.     /*
  21.        Cache whole database into system memory and share among other scripts & websites
  22.        WARNING: Please make sure your system have sufficient RAM to enable this feature
  23.     */
  24.     //$loc = new \IP2Location\Database('./databases/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-SAMPLE.BIN', \IP2Location\Database::SHARED_MEMORY);
  25.  
  26.     /*
  27.        Cache the database into memory to accelerate lookup speed
  28.        WARNING: Please make sure your system have sufficient RAM to enable this feature
  29.     */
  30.     //$loc = new \IP2Location\Database(ROOT . './databases/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-SAMPLE.BIN', \IP2Location\Database::MEMORY_CACHE);
  31.  
  32.     $ip = $_SERVER['REMOTE_ADDR'];
  33.  
  34.     //Retrieving data from database
  35.     $time_zone = $loc->lookup($ip, \IP2Location\Database::TIME_ZONE);
  36.     $country_code = $loc->lookup($ip, \IP2Location\Database::COUNTRY_CODE);
  37.     $latitude = $loc->lookup($ip, \IP2Location\Database::LATITUDE);
  38.     $longitude = $loc->lookup($ip, \IP2Location\Database::LONGITUDE);
  39.              
  40.  
  41.     //Setting Sun's zenith value
  42.     $zenith = 90+50/60;
  43.     $offset = explode(':', $time_zone);
  44.  
  45.     date_default_timezone_set("UTC");
  46.     $date = date_create("UTC");
  47.  
  48.     $sunset = date_sunset(date_timestamp_get($date), SUNFUNCS_RET_STRING, $latitude, $longitude, $zenith, $offset[0]);
  49.     $sunrise = date_sunrise(date_timestamp_get($date), SUNFUNCS_RET_STRING, $latitude, $longitude, $zenith, $offset[0]);
  50.  
  51.     //Displaying output
  52.     echo 'Country Code: ' . $country_code . '<br />';
  53.     echo 'Sunset Time: ' . $sunset . '<br />';
  54.     echo 'Sunrise Time: ' . $sunrise . '<br />';
  55.     echo 'Latitude: ' . $latitude . '<br />';
  56.     echo 'Longitude: ' . $longitude . '<br />';
  57. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement