date_sun_info

(PHP 5 >= 5.1.2, PHP 7, PHP 8)

date_sun_info日の出/日の入り時刻と薄明かり (twilight) の開始/終了時刻の情報を含む配列を返す

説明

date_sun_info(int $timestamp, float $latitude, float $longitude): array

パラメータ

timestamp

Unixタイムスタンプ。

latitude

緯度を表す度数。

longitude

経度を表す度数。

戻り値

成功した場合に配列、失敗した場合に false を返します。 配列の構造の詳細は、以下のリストのとおりです

sunrise
日の出のタイムスタンプ (天頂角 = 90°50' )
sunset
日の入りのタイムスタンプ (天頂角 = 90°35' )
transit
太陽が天頂に達するタイムスタンプ。 つまり、もっとも上部に達した時刻です。
civil_twilight_begin
夜明けの始まり(天頂角 = 96°) sunrise の時刻に終了します。
civil_twilight_end
夕暮れの終わり(天頂角 = 96°) sunset の時刻に始まります。
nautical_twilight_begin
航海上の夜明け (天頂角 = 102°) civil_twilight_begin に終了します。
nautical_twilight_end
航海上の夕暮れ (天頂角 = 102°) civil_twilight_end の時刻に始まります。
astronomical_twilight_begin
天文学上の夜明け (天頂角 = 108°) nautical_twilight_begin に終了します。
astronomical_twilight_end
天文学上の夕暮れ (天頂角 = 108°) nautical_twilight_end の時刻に始まります。

配列の要素の値は、UNIXタイムスタンプ、または 太陽が天頂より一日中下にある場合、false です。 また、一日中天頂より上にある場合、 true です。

変更履歴

バージョン 説明
7.2.0 ローカルの昼ではなく、 夜中に関する計算結果が修正されました。 これによって、結果が少し変わります。

例1 date_sun_info() の例

<?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

例2 極夜のデータに処理を少し行う例

<?php
$tz
= new \DateTimeZone('America/Anchorage');

$si = date_sun_info(strtotime("2022-12-21"), 70.21, -148.51);
foreach (
$si as $key => $value) {
echo
match (
$value) {
true => 'always',
false => 'never',
default =>
date_create("@{$value}")->setTimeZone($tz)->format( 'H:i:s T' ),
},
": {$key}",
"\n";
}
?>

上の例の出力は以下となります。

never: sunrise
never: sunset
12:52:18 AKST: transit
10:53:19 AKST: civil_twilight_begin
14:51:17 AKST: civil_twilight_end
09:01:47 AKST: nautical_twilight_begin
16:42:48 AKST: nautical_twilight_end
07:40:47 AKST: astronomical_twilight_begin
18:03:49 AKST: astronomical_twilight_end

例3 Midnight sun の例(Tromsø, Norway)

<?php
$si
= date_sun_info(strtotime("2022-06-26"), 69.68, 18.94);
print_r($si);
?>

上の例の出力は以下となります。

Array
(
    [sunrise] => 1
    [sunset] => 1
    [transit] => 1656240426
    [civil_twilight_begin] => 1
    [civil_twilight_end] => 1
    [nautical_twilight_begin] => 1
    [nautical_twilight_end] => 1
    [astronomical_twilight_begin] => 1
    [astronomical_twilight_end] => 1
)

例4 一日の長さを計算する例 (Kyiv)

<?php
$si
= date_sun_info(strtotime('2022-08-26'), 50.45, 30.52);
$diff = $si['sunset'] - $si['sunrise'];
echo
"Length of day: ",
floor($diff / 3600), "h ",
floor(($diff % 3600) / 60), "s\n";
?>

上の例の出力は以下となります。

Length of day: 13h 56s

add a note add a note

User Contributed Notes 4 notes

up
3
ELY M.
13 years ago
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);
}
?>
up
1
nospam at nomail dot com
6 years ago
maybe I am wrong, but I think

SUNFUNCS_RET_TIMESTAMP     return GMT(0) time

SUNFUNCS_RET_STRING     Return local time
SUNFUNCS_RET_DOUBLE     Return local time
up
1
mother at localsnow dot com
13 years ago
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]

* * *
up
1
info at mobger dot de
3 years ago
The relation between timestamp and geoposition is not good defined.
My try of a definition is:

date_sun_info —
Returns an array with information about sunset/sunrise and twilight begin/end as Unix-Timestamp for the the geoposition, which must have the same (local) date as the timestamp in the parameter-block for the function `date_sun_info`.

<?php
<?php
$tStamp 
= strtotime('2020-12-04');
$latitude = 50;
echo(
"\n");
foreach([-
181,-180,0,180,360] as $longitude ) {
    foreach([-
86401,-86400,-86399, -1,0,1,86399, 86400,86401] as $variTimeStamp) {

       
$sunInfoList = date_sun_info(($tStamp-$variTimeStamp),$latitude, $longitude);
       
$sunrise = new DateTime('@'.$sunInfoList['sunrise']);
        echo(
$sunInfoList['sunrise']. ' => '.$sunrise->format('H:i:s d.m.Y').' || [ '.$variTimeStamp.' // ' . $longitude.'° ]');
        echo(
"\n");
    }   
    echo(
"\n");
}

?>

You may recognize the equivalence of `[ 0 // 360° ]` and `[ 86400 // 0° ]` in the results.
The result is:
<?php
/**
1607197612 => 19:46:52 05.12.2020 || [ -86401 // -181° ]
1607197612 => 19:46:52 05.12.2020 || [ -86400 // -181° ]
1607111141 => 19:45:41 04.12.2020 || [ -86399 // -181° ]
1607111141 => 19:45:41 04.12.2020 || [ -1 // -181° ]
1607111141 => 19:45:41 04.12.2020 || [ 0 // -181° ]
1607024668 => 19:44:28 03.12.2020 || [ 1 // -181° ]
1607024668 => 19:44:28 03.12.2020 || [ 86399 // -181° ]
1607024668 => 19:44:28 03.12.2020 || [ 86400 // -181° ]
1606938194 => 19:43:14 02.12.2020 || [ 86401 // -181° ]

1607197372 => 19:42:52 05.12.2020 || [ -86401 // -180° ]
1607197372 => 19:42:52 05.12.2020 || [ -86400 // -180° ]
1607110901 => 19:41:41 04.12.2020 || [ -86399 // -180° ]
1607110901 => 19:41:41 04.12.2020 || [ -1 // -180° ]
1607110901 => 19:41:41 04.12.2020 || [ 0 // -180° ]
1607024428 => 19:40:28 03.12.2020 || [ 1 // -180° ]
1607024428 => 19:40:28 03.12.2020 || [ 86399 // -180° ]
1607024428 => 19:40:28 03.12.2020 || [ 86400 // -180° ]
1606937953 => 19:39:13 02.12.2020 || [ 86401 // -180° ]

1607154137 => 07:42:17 05.12.2020 || [ -86401 // 0° ]
1607154137 => 07:42:17 05.12.2020 || [ -86400 // 0° ]
1607067665 => 07:41:05 04.12.2020 || [ -86399 // 0° ]
1607067665 => 07:41:05 04.12.2020 || [ -1 // 0° ]
1607067665 => 07:41:05 04.12.2020 || [ 0 // 0° ]
1606981191 => 07:39:51 03.12.2020 || [ 1 // 0° ]
1606981191 => 07:39:51 03.12.2020 || [ 86399 // 0° ]
1606981191 => 07:39:51 03.12.2020 || [ 86400 // 0° ]
1606894715 => 07:38:35 02.12.2020 || [ 86401 // 0° ]

1607197301 => 19:41:41 05.12.2020 || [ -86401 // 180° ]
1607197301 => 19:41:41 05.12.2020 || [ -86400 // 180° ]
1607110828 => 19:40:28 04.12.2020 || [ -86399 // 180° ]
1607110828 => 19:40:28 04.12.2020 || [ -1 // 180° ]
1607110828 => 19:40:28 04.12.2020 || [ 0 // 180° ]
1607024353 => 19:39:13 03.12.2020 || [ 1 // 180° ]
1607024353 => 19:39:13 03.12.2020 || [ 86399 // 180° ]
1607024353 => 19:39:13 03.12.2020 || [ 86400 // 180° ]
1606937877 => 19:37:57 02.12.2020 || [ 86401 // 180° ]

1607154065 => 07:41:05 05.12.2020 || [ -86401 // 360° ]
1607154065 => 07:41:05 05.12.2020 || [ -86400 // 360° ]
1607067591 => 07:39:51 04.12.2020 || [ -86399 // 360° ]
1607067591 => 07:39:51 04.12.2020 || [ -1 // 360° ]
1607067591 => 07:39:51 04.12.2020 || [ 0 // 360° ]
1606981115 => 07:38:35 03.12.2020 || [ 1 // 360° ]
1606981115 => 07:38:35 03.12.2020 || [ 86399 // 360° ]
1606981115 => 07:38:35 03.12.2020 || [ 86400 // 360° ]
1606894638 => 07:37:18 02.12.2020 || [ 86401 // 360° ]

*/
?>
To Top