One thing that has gotten me into trouble recently was PHP's date_sun_info() function. It returns an array with information about daylight such as sunrise and sunset but also various twilight phases. However, when using this function it appears as if PHP always calculates the appropriate parameters chronologically. This can create a problem depending on the timezone you want these parameters for.
Let's take these two examples (assuming we're using UTC as default timezone, i.e. date_default_timezone_set("UTC")
):
date_sun_info(strtotime("2013-01-01"),31.5,35.2)
returns
sunrise: 2013-01-01 04:38:39
sunset: 2013-01-01 14:47:28
transit: 2013-01-01 09:43:03
civil_twilight_begin: 2013-01-01 04:12:02
civil_twilight_end: 2013-01-01 15:14:05
nautical_twilight_begin: 2013-01-01 03:41:47
nautical_twilight_end: 2013-01-01 15:44:20
astronomical_twilight_begin: 2013-01-01 03:12:11
astronomical_twilight_end: 2013-01-01 16:13:56
Note how all the cacluated parameters neatly fall into the calendar date that was passed to the function. But now let's look at values for the other side of the globe:
date_sun_info(strtotime("2013-01-01"),-44.5,-176.2)
which returns
sunrise: 2013-01-01 16:05:13
sunset: 2013-01-02 07:32:39
transit: 2013-01-01 23:48:56
civil_twilight_begin: 2013-01-01 15:28:55
civil_twilight_end: 2013-01-02 08:08:56
nautical_twilight_begin: 2013-01-01 14:41:04
nautical_twilight_end: 2013-01-02 08:56:47
astronomical_twilight_begin: 2013-01-01 13:40:01
astronomical_twilight_end: 2013-01-02 09:57:50
Obviously, some of the parameters do not fall into the calendar date fed to the function but are calculated for the specified timezone. I noticed that this has thrown other people off in the past as well. However, in some cases what is desired is to get the times of the different phases of the day for the specified calendar date. In the case of above example the output would have to look like this:
transit: 2013-01-01 00:48:26
sunset: 2013-01-01 08:32:38
civil_twilight_end: 2013-01-01 09:09:01
nautical_twilight_end: 2013-01-01 09:57:01
astronomical_twilight_end: 2013-01-01 10:58:26
astronomical_twilight_begin: 2013-01-01 14:39:57
nautical_twilight_begin: 2013-01-01 15:41:01
civil_twilight_begin: 2013-01-01 16:28:52
sunrise: 2013-01-01 17:05:10
I have written a custom function to get around this:
function adjustedSunInfo($date,$lat,$lon) {
$sinfo=date_sun_info ( strtotime($date) ,$lat,$lon );
$sinfoMinus=date_sun_info ( strtotime($date)-86400 ,$lat,$lon );
$sinfoPlus=date_sun_info ( strtotime($date)+86400 ,$lat,$lon );
foreach($sinfo as $key=>$val) {
if(date("d",$val)>date("d",strtotime($date))) {
$sinfo[$key]=$sinfoMinus[$key];
} else if(date("d",$val)>date("d",strtotime($date))) {
$sinfo[$key]=$sinfoPlus[$key];
}
}
asort($sinfo);
return $sinfo;
}
However, the question is whether there are an (undocumented) flags that can be passed to PHP - or any other tricks - to get the desired info?
See Question&Answers more detail:
os