LoginSignup
0
0

More than 1 year has passed since last update.

【PHP】日出、日没、薄明の時間を取得してみた

Last updated at Posted at 2021-10-26

初めに

ダッシュボード作成のために、各種情報を取得する
コメントより、date_sun_info関数にて取得できることが判明
⇒PHPを用い、HP等で表示させる用途を想定

Sunset and sunrise times APIを使用予定でした、、

環境

  • Windows10
  • PHP 8.0.6

パラメータ

lat (float): 取得したい場所の緯度、必須
lng (float): 取得したい場所の経度、必須
date (string): YYYY-MM-DDフォーマットにて入力/相対・絶対日時の指定も可能。入力しない場合は現在の時刻を取得する。オプション。
callback (string): Callback function name for JSONP response. Optional.
formatted (integer): 0 か 1 (1 がデフォルト). 0の場合はISO 8601にて出力される。オプション

本コードでは$sunset_configにてパラメータをセットしてください。

コード

sunset_time_api.php
<?php
//sunset_apiより日没、薄明時間を取得する
date_default_timezone_set('Asia/Tokyo');
$sunset_config = array(
    'lat' => '35.00000',
    'lon' => '135.0000000',
    'date' => 'today',
    'formatted' => '0',
);
$sunset_json = file_get_contents("https://api.sunrise-sunset.org/json?lat=" . $sunset_config["lat"] . "&lng=" . $sunset_config["lon"] . "&date=" . $sunset_config["date"] . "&formatted=" . $sunset_config["formatted"]);
$sunset_array = json_decode($sunset_json, true);

//UTC => Timestamp
$utc_sunrise = strtotime($sunset_array["results"]["sunrise"]);
$utc_sunset = strtotime($sunset_array["results"]["sunset"]);
$utc_civil_twilight_end = strtotime($sunset_array["results"]["civil_twilight_end"]);
$utc_nautical_twilight_end = strtotime($sunset_array["results"]["nautical_twilight_end"]);
$utc_astronomical_twilight_end = strtotime($sunset_array["results"]["astronomical_twilight_end"]);

//UTC => JST
$sunrise = date("m/d H:i", $utc_sunrise);
$sunset = date("m/d H:i", $utc_sunset);
$civil_twilight_end = date("m/d H:i", $utc_civil_twilight_end);
$nautical_twilight_end = date("m/d H:i", $utc_nautical_twilight_end);
$astronomical_twilight_end = date("m/d H:i", $utc_astronomical_twilight_end);

echo "日の出" . $sunrise . "\n";
echo "日の入り" . $sunset . "\n";
echo "市民薄明の終わり" . $civil_twilight_end . "\n";
echo "航海薄明の終わり" . $nautical_twilight_end . "\n";
echo "天文薄明の終わり" . $astronomical_twilight_end . "\n";

参考サイト

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0