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); 


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!




PHP Phrasebook
PHP Phrasebook
ISBN: 0672328178
EAN: 2147483647
Year: 2005
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net