Determining Sunrise and Sunset

date_sunrise(time(), SUNFUNCS_RET_STRING, 48, 11.5, 90, 1);
date_sunset(time(), SUNFUNCS_RET_STRING, 48, 11.5, 90, 1);

date_sunrise and date_sunset functions syntax

mixed date_sunrise ( int $timestamp
	[,int $format = SUNFUNCS_RET_STRING
	[, float $latitude = ini_get("date.default_latitude")
	[, float $longitude = ini_get("date.default_longitude") 
	[, float $zenith = ini_get("date.sunrise_zenith")
	[, float $gmt_offset = 0 ]]]]] )

These functions return time of sunset/sunrise (specified as a timestamp) for a given day and location. This function accepted following parameters:

  1. timestamp timestamp of the day

  2. format

    1. SUNFUNCS_RET_STRING returns string
    2. SUNFUNCS_RET_DOUBLE returns float
    3. SUNFUNCS_RET_TIMESTAMP returns integer (timestamp)

  3. latitude default north

  4. longitude default east

  5. zenith

  6. gmtoffset in hours

Depending on the current location and date, the times for sunrise and sunset can drastically vary. However, formulas exist for determining this value depending on latitude and longitude, and PHP has this functionality integrated into its core starting with PHP 5. All you are required to do is call date_sunrise() and date_sunset(). Both functions expect a number of parameters:

  • A time stamp (epoche value) of the date for which to determine the sunrise/sunset

  • The desired format for the return value: SUFUNCS_RET_DOUBLE returns the time as a float value (between 0 and 23.99), SUNFUNCS_RET_STRING returns it as a string (between 00:00 and 23:59), and SUNFUNCS_RET_TIMESTAMP returns an epoche value

  • The latitude (Northern latitude; use negative values for a Southern latitude)

  • The longitude (Eastern longitude; use negative values for a Western longitude)

  • The zenith of the sunrise (in degrees)

  • The offset (in hours) to Greenwich mean time (GMT)

So, the preceding code (in sun.php)calculates the sunrise and sunset for Munich, Germany, which resides at about 48 Northern latitude, 11 30' Eastern longitude, for the current day. I checked it: It worked!

by BrainBellupdated