Display Sunrise and Sunset Time Using PHP and MySQL Database

In this tutorial, we demonstrate you on how to display visitor's sunrise and sunset time based on their IP address using PHP programming languages and IP2Location MySQL database. In this tutorial, we use the IP2Location LITE database to lookup country of origin from the visitor's IP address. Free databases are available for download at IP2Location LITE database.

 

Below are the steps to set up the database for both IPv4 and IPv6 data and the sample codes

Step 1: Download IP2Location LITE database, unzip the file follow the instruction in order to create database table. Please refer to DB11 LITE for further information.

Step 2: Run the below sample code.

<?php
//MySQL configuration
$mysql_server = "mysql_server.com";
$mysql_user_name = "User ID";
$mysql_user_pass = "password";

$ip = $_SERVER['REMOTE_ADDR'];
$ipno = Dot2LongIP($ip);

$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database"); 
// Connect to the IP2Location database
mysql_select_db("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_from AND $ipno <= ip_to"; 
// Execute SQL query
$result = mysql_query($query) or die("IP2Location Query Failed"); 
// Retrieve the recordset (only one)
$row = mysql_fetch_object($result); 

//Retrieving data from database
$country_code = $row->country_code;
$time_zone = $row->time_zone;
$latitude = $row->latitude;
$longitude = $row->longitude;

mysql_free_result($result); mysql_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 (xxx.xxx.xxx.xxx) to IP number (0 to 256^4-1)
function Dot2LongIP ($IPaddr) {
	if ($IPaddr == "")
	{
	return 0;
	}	 
	else {
	$ips = explode(".", $IPaddr);
	return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
	}
}
?>

Step 1: Download IP2Location LITE database, unzip the file follow the instruction in order to create database table. Please refer to DB11 LITE for further information.

Step 2: Run the below sample code.

<?php
//MySQL configuration
$mysql_server = "mysql_server.com";
$mysql_user_name = "User ID";
$mysql_user_pass = "password";

$ip = $_SERVER['REMOTE_ADDR'];
$ipno = Dot2LongIPv6($ip);

$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database"); 
// Connect to the IP2Location database
mysql_select_db("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_from AND $ipno <= ip_to"; 
// Execute SQL query
$result = mysql_query($query) or die("IP2Location Query Failed"); 
// Retrieve the recordset (only one)
$row = mysql_fetch_object($result); 

//Retrieving data from database
$country_code = $row->country_code;
$time_zone = $row->time_zone;
$latitude = $row->latitude;
$longitude = $row->longitude;

mysql_free_result($result); mysql_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;
}
?>

Do you like this article? Share it with others by clicking the social media buttons below. We will write more articles related to this topic.