Date_sun_info () for a specific calendar date

Recently, I have encountered the PHP date_sun_info () problem . It returns an array with daylight information, such as sunrise and sunset, as well as various phases of twilight. However, when using this function, it seems that PHP always calculates the corresponding parameters in chronological order. This can create a problem depending on the time zone for which you want to use these settings.

Let's look at these two examples (suppose we use UTC as the default time zone, 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

, cacluited , . :

date_sun_info(strtotime("2013-01-01"),-44.5,-176.2)

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

, , , . , . . :

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

, :

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

, () , PHP, , ?

0
1

, 100%, , , . .

, , PHP date_sun_info 1 .

, : -

() , PHP, , ?

(, ), . - GMT (, ), , , . DateTime .

: -

/**
* Get an array of human readable times for sun events
* @param \DateTime $date A \DateTime Object set to the desired date
*                        and the correct timezone for the place in question
* @param float $lat The latitude of the place in question
* @param float $lon The longitude of the place in question
*
* @return array An associative array of human readable times sorted in chronological order.
*/

function adjustedSunInfo(\DateTime $date,$lat,$lon) {
    $sinfo=date_sun_info ($date->getTimestamp() ,$lat,$lon );

    foreach($sinfo as $key=>$val) {
        //You should check that $val isn't 1 or empty first
        $time = new \DateTime('@' . $val);
        $time->setTimezone($date->getTimeZone());
        $sinfo[$key] = $time->format('Y m d H:i:s');
    }

    asort($sinfo);
    return $sinfo;
}

$time = new \DateTime('2013-01-01', new \DateTimeZone('Pacific/Chatham'));
var_dump(adjustedSunInfo($time, -44.5, -176.2));

, .

, . , .

DST Leap Years , , , , .

, .

1. 1 .

0

Source: https://habr.com/ru/post/1793386/


All Articles