# how to calculate sunrise and sunset time in php

#### by THavamani 2014-04-04 12:38:59

how to calculate sunrise and sunset time in php?

There is a default function in php to calculate sunrise and sunset time.

Sunrise and sunset time is given below

date_sunrise(date, SUNFUNCS_RET_STRING, latitude, longitude, zenith, tzoneoffset);
date_sunset(date, SUNFUNCS_RET_STRING, latitude, longitude, zenith, tzoneoffset);

Example:

// Set the timezone for your location, because some servers are in different timezone than your location

\$myTZ = 'Asia/Kolkata';

// Set the latitude and longitude for your location
\$latitude =22.569722; //North
\$longitude =88.369722; //West

\$zenith = 90+(50/60); // True sunrise/sunset

// Set timezone in PHP5/PHP4 manner
if (!function_exists('date_default_timezone_set')) {
putenv("TZ=" . \$myTZ);
} else {
date_default_timezone_set("\$myTZ");
}

// find time offset in hours
\$tzoffset = date("Z")/60 / 60;

// determine sunrise time
\$sunrise_time= date_sunrise(strtotime("12-10-2014"), SUNFUNCS_RET_STRING, \$latitude, \$longitude, \$zenith, \$tzoffset);
echo "Sunrise time=".\$sunrise_time;

// determine sunset time
\$sunset_time = date_sunset((strtotime("12-10-2014"), SUNFUNCS_RET_STRING, \$latitude, \$longitude, \$zenith, \$tzoffset);

echo "Sunset time=".\$sunset_time;

Output

Sunrise time=05:35
Sunset time=17:06

This is the way to calculate sunrise and sunset time in php

933
0
0