date_sunset

(PHP 5, PHP 7, PHP 8)

date_sunset 指定した日付と場所についての日の入り時刻を返す

説明

date_sunset(
    int $timestamp,
    int $returnFormat = SUNFUNCS_RET_STRING,
    float|null $latitude = null,
    float|null $longitude = null,
    float|null $zenith = null,
    float|null $utcOffset = null
): string|int|float|false

date_sunset() は、与えられた日付 (timestamp で指定する) と場所についての日の入り時刻を返します。

パラメータ

timestamp

日の入り時刻を取得する日の timestamp

returnFormat

returnFormat 定数
定数 説明
SUNFUNCS_RET_STRING 結果を string で返します。 16:46
SUNFUNCS_RET_DOUBLE 結果を float で返します。 16.78243132
SUNFUNCS_RET_TIMESTAMP 結果を int (タイムスタンプ) で返します。 1095034606

latitude

デフォルトは北緯で、南緯は負の値で表します。 date.default_latitude も参照ください。

longitude

デフォルトは東経で、西経は負の値で表します。 date.default_longitude も参照ください。

zenith

zenith は 太陽の中心と、地球の表面からの垂線の間になす角度です。 デフォルトは date.sunset_zenith です。

一般的な zenith の角度
角度 説明
90°50' 日の入り: 太陽が見えなくなる点
96° 薄明かり: 夕暮れの終わりを示すのに慣習的に使われます
102° 航海上の薄明かり: 海の上で、水平線が見えなくなった点
108° 天文学上の薄明かり: 太陽があらゆる明かりの光源ではなくなった点

utcOffset

時間単位で指定します。 returnFormatSUNFUNCS_RET_TIMESTAMP の場合は、 utcOffset は無視されます。

返り値

日の入り時刻を、指定した returnFormat で返します。 失敗した場合に false を返します 失敗する潜在的な可能性があります。太陽が全く昇らない場合です。 これは一年のある時期、極圏の中にある場合に起こります。

エラー / 例外

すべての日付/時刻関数は、 有効なタイムゾーンが設定されていない場合に E_NOTICE を発生させます。また、システム設定のタイムゾーンあるいは環境変数 TZ を使用した場合には E_STRICT あるいは E_WARNING を発生させます。 date_default_timezone_set() も参照ください。

変更履歴

バージョン 説明
8.0.0 latitude, longitude, zenith, utcOffset は、nullable になりました。

例1 date_sunset() の例

<?php

/* ポルトガル リスボンの日の入り時刻を計算する
緯度: 北緯 38.4
経度: 西経 9
天頂 ~= 90
時差: +1 GMT
*/

echo date("D M d Y"). ', sunset time : ' .date_sunset(time(), SUNFUNCS_RET_STRING38.4, -9901);

?>

上の例の出力は、 たとえば以下のようになります。

Mon Dec 20 2004, sunset time : 18:13

例2 No sunset

<?php
$solstice 
strtotime('2017-12-21');
var_dump(date_sunset($solsticeSUNFUNCS_RET_STRING69.245833, -53.537222));
?>

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

bool(false)

参考

  • date_sunrise() - 指定した日付と場所についての日の出時刻を返す
  • date_sun_info() - 日の出/日の入り時刻と薄明かり (twilight) の開始/終了時刻の情報を含む配列を返す

add a note add a note

User Contributed Notes 3 notes

up
1
matt at mctsoft dot net
4 years ago
yes SUNFUNCS_RET_TIMESTAMP does return GMT(0) time

so something like...

$arr = localtime(date_sunset(time(),SUNFUNCS_RET_TIMESTAMP,51.5,0)); // London

$hh = $arr[2];
$mm = $arr[1];

Will give figure out your localtime and daylight saving (BST)
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
-25
michael at dayah dot com
17 years ago
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.

<?php
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";
}
?>
To Top