©
このドキュメントでは、 php中国語ネットマニュアル リリース
(PHP 5 >= 5.1.2)
date_sun_info — Returns an array with information about sunset/sunrise and twilight begin/end
$time
, float $latitude
, float $longitude
)
time
Timestamp.
latitude
Latitude in degrees.
longitude
Longitude in degrees.
Returns array on success 或者在失败时返回 FALSE
.
Example #1 A date_sun_info() example
<?php
$sun_info = date_sun_info ( strtotime ( "2006-12-12" ), 31.7667 , 35.2333 );
foreach ( $sun_info as $key => $val ) {
echo " $key : " . date ( "H:i:s" , $val ) . "\n" ;
}
?>
以上例程会输出:
sunrise: 05:52:11 sunset: 15:41:21 transit: 10:46:46 civil_twilight_begin: 05:24:08 civil_twilight_end: 16:09:24 nautical_twilight_begin: 04:52:25 nautical_twilight_end: 16:41:06 astronomical_twilight_begin: 04:21:32 astronomical_twilight_end: 17:12:00
[#1] gwschnell + gmail ! com [2015-03-05 22:32:13]
In extreme regions, the return value might be true or false. true means the event will always occur, e.g. the sun always has a positive elevation. false means the event will never occur, e.g. the sun has always a negative elevation.
[#2] la_grief at hotmail dot com [2013-02-15 20:12:07]
While the bug has been fixed in future versions, be aware that in some PHP versions around 5.1-5.2 have a situation where the latitude and longitude are read in reverse. This will cause the output to be for a completely different location on Earth.
array date_sun_info ( int $time , float $latitude , float $longitude )
wrongly becomes
array date_sun_info ( int $time , float $longitude, float $latitude)
Make sure you do a double check before deploying this function to a live state, and verify against date_sunrise() or date_sunset() that you don't have to put the location's real longitude in the function's latitude parameter and vice versa.
Just to reiterate, THIS HAS BEEN FIXED! But in some versions of PHP that may be out there on shared hosting solutions, you may be getting odd results, and this will most likely be the reason. I hope this helps others that are searching for a solution quickly.
[#3] ELY M. [2010-10-08 09:42:03]
I have been working on my own php script to get current down or up for sun and moon. I had to add function for any places that have 24 hour sun.
here is my code for places with 24 hour sun.
<?php
if ($sunrise == 0 && $sunset == 0) {
$sunrise24 = "";
$sunset24 = "";
//run suninfo
$sunup = date_sun_info(strtotime($year."-".$month."-".$day), $lat, $lon);
}
//check if sun is up all day.
if ($sunup[sunrise] == 1 && $sunup[sunrise] == 1) {
imagecopy($sky, $sun, 60, 20, 0, 0, $sun_width, $sun_height);
imagefill($sky, 0, 0, $bluesky);
}
?>
[#4] mother at localsnow dot com [2010-06-23 14:55:05]
We needed the length of the day, both sunrise to sunset and twilight to twilight for particular latitudes. Sun_info() is just the thing. We mistakenly thought 'transit' was this value, which it is not. Transit is the time of day the sun is at its zenith. To get length of day, one must perform math on the results of sun_info().
When doing math with time values, don't expect date() to do the conversion to hours:minutes:seconds. date() thinks the passed value is a time since the epoch. You will need to do your own conversion to hours:minutes:seconds, using something like the following:
<?php
function hms($val) {
// convert seconds to hours:minutes:seconds
$v=$val;
$h=intval($v/3600);
$v-=($h*3600); // subtract hours
$m=intval($v/60);
$v-=($m*60); // subtract minutes
$s=$v % 60; // seconds remaining
if ($h<10) {$h="0".$h;}
if ($m<10) {$m="0".$m;}
if ($s<10) {$s="0".$s;}
return $h.":".$m.":".$s;
}
?>
Regarding date_sunrise() and date_sunset(), these both return values without seconds and without correction for Daylight time. Whereas sun_info() handles seconds as well as Daylight time. It even handles dates prior to the epoch correctly as negative timestamps, at least as of php 5.2.12
For example,
sun_info(strtotime('July 4, 1776'),47.3506,-122.6417)
produces something like the following when using date_default_timezone_set('America/Los_Angeles') and
date("H:i:s", $val)
sunrise: 04:20:26 [-6106016374]
sunset: 20:09:03 [-6105959457]
transit: 12:14:45 [-6105987915]
civil_twilight_begin: 03:40:54 [-6106018746]
civil_twilight_end: 20:48:35 [-6105957085]
nautical_twilight_begin: 02:46:58 [-6106021982]
nautical_twilight_end: 21:42:31 [-6105953849]
astronomical_twilight_begin: 01:28:06 [-6106026714]
astronomical_twilight_end: 23:01:23 [-6105949117]
* * *
[#5] glenbo (_AT_) mac (_DOT_) com [2008-06-17 12:54:27]
It should be noted that for extreme geographical locations date_sun_info() might return unexpected values. Values of 1 or empty may be returned. If you are expecting a unix timestamp this will default to the epoch, or epoch+1, which is not what you would expect.
After researching official almanac records for these locations it appears likely that for sunrise and sunset return values of 1 relate to a situation where the sun is above the horizon for the entire 24 hour day. It is also possible that empty return values relate to a situation where the sun is below the horizon for the entire 24 hour day. In the case of twilight data a 1 probably means that the sun never dips below that zenith, and an empty value means the sun never rises above said zenith for that given day.
The following code exhibits unique dates from the northernmost city Ny-?lesund, Svalbard, and the southernmost city McMurdo Research Station, Antarctica.
<?php
$northernmost_city_latitude = 78.92; // Ny-?lesund, Svalbard
$northernmost_city_longitude = 11.93;
$southernmost_city_latitude = -77.88; // McMurdo Research Station, Antarctica
$southernmost_city_longitude = 166.73;
print_r( date_sun_info( strtotime("2008-01-01") , $northernmost_city_latitude, $northernmost_city_longitude) );
print_r( date_sun_info( strtotime("2008-04-01") , $northernmost_city_latitude, $northernmost_city_longitude) );
print_r( date_sun_info( strtotime("2008-01-01") , $southernmost_city_latitude, $southernmost_city_longitude) );
print_r( date_sun_info( strtotime("2008-06-01") , $southernmost_city_latitude, $southernmost_city_longitude) );
?>
This will return the following. Observe that sometimes a value of 1 or empty is returned.
Array
(
[sunrise] =>
[sunset] =>
[transit] => 1199186158
[civil_twilight_begin] =>
[civil_twilight_end] =>
[nautical_twilight_begin] => 1199184075
[nautical_twilight_end] => 1199188241
[astronomical_twilight_begin] => 1199170475
[astronomical_twilight_end] => 1199201840
)
Array
(
[sunrise] => 1207019232
[sunset] => 1207077865
[transit] => 1207048548
[civil_twilight_begin] => 1
[civil_twilight_end] => 1
[nautical_twilight_begin] => 1
[nautical_twilight_end] => 1
[astronomical_twilight_begin] => 1
[astronomical_twilight_end] => 1
)
Array
(
[sunrise] => 1
[sunset] => 1
[transit] => 1199148994
[civil_twilight_begin] => 1
[civil_twilight_end] => 1
[nautical_twilight_begin] => 1
[nautical_twilight_end] => 1
[astronomical_twilight_begin] => 1
[astronomical_twilight_end] => 1
)
Array
(
[sunrise] =>
[sunset] =>
[transit] => 1212281461
[civil_twilight_begin] =>
[civil_twilight_end] =>
[nautical_twilight_begin] => 1212273312
[nautical_twilight_end] => 1212289609
[astronomical_twilight_begin] => 1212264187
[astronomical_twilight_end] => 1212298734
)