Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Date and Time Functions : date_sunset

date_sunset

Returns time of sunset for a given day and location (PHP 5)
mixed date_sunset ( int timestamp [, int format [, float latitude [, float longitude [, float zenith [, float gmt_offset]]]]] )

Example 444. date_sunset() example

<?php

/* calculate the sunset time for Lisbon, Portugal
Latitude: 38.4 North
Longitude: 9 West
Zenith ~= 90
offset: +1 GMT
*/

echo date("D M d Y"). ', sunset time : ' .date_sunset(time(), SUNFUNCS_RET_STRING, 38.4, -9, 90, 1);

?>

The above example will output something similar to:

Mon Dec 20 2004, sunset time : 18:13

Code Examples / Notes » date_sunset

michael

I use an IP to location database to determine the visitor's approximate latitude and longitude and then serve them a day or night color scheme based on whether it is before civil dawn or dusk. I've had problems when not specifying the timezone, specifically a 1 hour error, so I use GMT.
<?
date_default_timezone_set("GMT");
function scheme() {
       $sunrise = date_sunrise(time(), SUNFUNCS_RET_DOUBLE, $latitude, $longitude, 96, 0);
       $sunset = date_sunset(time(), SUNFUNCS_RET_DOUBLE, $latitude, $longitude, 96, 0);
       $now = date("H") + date("i") / 60 + date("s") / 3600;
       if ($sunrise < $sunset)
               if (($now > $sunrise) && ($now < $sunset)) return "day";
               else return "night";
       else
               if (($now > $sunrise) || ($now < $sunset)) return "day";
               else return "night";
}
?>


jbr

Here a function that will return an array of all valid formats, both starting and ending times!
[Editor's note: you can find that function on the docpage of date_sunrise()]


djwice

A way to use this:
<?php
// De Bilt, The Netherlands, weather station #06260
$lat = 52.10; // North
$long = 5.18; // East
$offset = 1; // difference between GMT and local time in hours
$zenith=90+50/60;
echo "


Sunrise: ".date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
echo "
Sunset: ".date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
$zenith=96;
echo "


\"Civilian Twilight\" start: ".date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
echo "
\"Civilian Twilight\" end: ".date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
$zenith=102;
echo "


\"Nautical Twilight\" start: ".date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
echo "
\"Nautical Twilight\" end: ".date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
$zenith=108;
echo "


\"Astronomical Twilight\" start: ".date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
echo "
\"Astronomical Twilight\" end: ".date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
?>  
Thanks to George King.
More sources about this topic:
http://www.qarlos.free.fr/navegacion/Sextante/formulas.htm
http://williams.best.vwh.net/sunrise_sunset_algorithm.htm
http://www.kevinboone.com/suntimes.html
http://star-www.st-and.ac.uk/~fv/webnotes/chapt12.htm


Change Language


Follow Navioo On Twitter
checkdate
date_create
date_date_set
date_default_timezone_get
date_default_timezone_set
date_format
date_isodate_set
date_modify
date_offset_get
date_parse
date_sun_info
date_sunrise
date_sunset
date_time_set
date_timezone_get
date_timezone_set
date
getdate
gettimeofday
gmdate
gmmktime
gmstrftime
idate
localtime
microtime
mktime
strftime
strptime
strtotime
time
timezone_abbreviations_list
timezone_identifiers_list
timezone_name_from_abbr
timezone_name_get
timezone_offset_get
timezone_open
timezone_transitions_get
eXTReMe Tracker