hexasoft icon

Display Sunrise and Sunset Time for IPV6

hexasoft | PRO | 05/16/18 08:21:27 AM UTC | 0 ⭐ | 247 👁️ | Never ⏰ | []
PHP |

3.24 KB

|

None

|

0 👍

/

0 👎

/****************************************************************************************************************/
/* Description: This snippet is provide simple way to help user display Visitor's Sunrise and Sunset Time.      */
/*              suggestions for different city locations across the country.                                    */
/*              There are 1 steps in this snippet. For information, please visit IP2Location tutorial page at:  */
/*https://www.ip2location.com/tutorials/display-sunrise-sunset-time-using-php-and-mysql-database                */
/****************************************************************************************************************/
/* You can obtain free IP2Location LITE database for IPv6 at https://lite.ip2location.com/                      */
 
<?php
    // MySQL configuration
    $mysql_server = "mysql_server.com";
    $mysql_user_name = "User ID";
    $mysql_user_pass = "password";
 
    $ip = $_SERVER['REMOTE_ADDR'];
    $ipno = Dot2LongIPv6($ip);
         
    // Connect to the database server
    $link = mysqli_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");
 
    // Connect to the IP2Location database
    mysqli_select_db($link,"ip2location") or die("Could not select database");
 
    // SQL query string to match the recordset that the IP number fall between the valid range
    $query = "SELECT time_zone, country_code, latitude, longitude FROM db11lite WHERE $ipno <?= ip_to LIMIT 1";
     
    // Execute SQL query
    $result = mysqli_query($link,$query) or die("IP2Location Query Failed");
 
    // Retrieve the recordset (only one)
    $row = mysqli_fetch_assoc($result);
     
    // Retrieving data from database
    $country_code = $row['country_code'];
    $time_zone = $row['time_zone'];
    $latitude = $row['latitude'];
    $longitude = $row['longitude'];
 
    // Free recordset and close database connection
    mysqli_free_result($result); 
    mysqli_close($link);
     
    // Setting Sun's zenith value
    $zenith = 90+50/60;
    $offset = explode(':', $time_zone);
 
    date_default_timezone_set("UTC");
    $date = date_create("UTC");
 
    $sunset = date_sunset(date_timestamp_get($date), SUNFUNCS_RET_STRING, $latitude, $longitude, $zenith, $offset[0]);
    $sunrise = date_sunrise(date_timestamp_get($date), SUNFUNCS_RET_STRING, $latitude, $longitude, $zenith, $offset[0]);
 
    //Displaying output
    echo 'Country Code: ' . $country_code . '<br />';
    echo 'Sunset Time: ' . $sunset . '<br />';
    echo 'Sunrise Time: ' . $sunrise . '<br />';
    echo 'Latitude: ' . $latitude . '<br />';
    echo 'Longitude: ' . $longitude . '<br />';
    echo $time_zone;
 
    // Function to convert IP address to IP number (IPv6)
    function Dot2LongIPv6 ($IPaddr) {
        $int = inet_pton($IPaddr);
        $bits = 15;
        $ipv6long = 0;
        while($bits >= 0){
            $bin = sprintf("%08b", (ord($int[$bits])));
            if($ipv6long){
                $ipv6long = $bin . $ipv6long;
            }
            else{
                $ipv6long = $bin;
            }
            $bits--;
        }
        $ipv6long = gmp_strval(gmp_init($ipv6long, 2), 10);
        return $ipv6long;
    }
?>

Comments