工作中遇到了问题,根据日出日落时间开关灯,可是如何确定当地的日出日落时间呢?
我们自然会想到调用Api;其实在PHP中有相关的函数:
定义和用法
date_sun_info()
函数返回一个包含有关指定日期与地点的日出/日落和黄昏开始/黄昏结束的信息的数组。
提示:请参阅 date_sunrise()
函数,返回指定日期与地点的日出时间。
提示:请参阅 date_sunset()
函数,返回指定日期与地点的日落时间。
语法
date_sun_info(timestamp,latitude,longitude);
参数 | 描述 |
---|---|
timestamp | 必需。规定时间戳。 |
latitude | 必需。规定纬度。 |
longitude | 必需。规定经度。 |
返回值
返回值: 如果成功则返回一个数组,如果失败则返回 FALSE。
代码示例
/**
* @param float $latitude 规定纬度。
* @param float $longitude 规定经度
* 如果成功则返回一个数组,如果失败则返回 FALSE。
*/
protected function dateSunInfo($latitude=31.7667,$longitude=35.2333)
{
//date_sun_info(timestamp,latitude,longitude);
$sun_info=date_sun_info(time(),$latitude,$longitude);
foreach ($sun_info as $key=>$val)
{
echo "$key: " . date("H:i:s",$val) . "<br>";
echo "$key: " . $val . "<br>";
echo "<br>";
}
}
运行结果
sunrise: 11:52:42
sunrise: 1520826762
sunset: 23:44:52
sunset: 1520869492
transit: 17:48:47
transit: 1520848127
civil_twilight_begin: 11:28:28
civil_twilight_begin: 1520825308
civil_twilight_end: 00:09:06
civil_twilight_end: 1520870946
nautical_twilight_begin: 11:00:13
nautical_twilight_begin: 1520823613
nautical_twilight_end: 00:37:21
nautical_twilight_end: 1520872641
astronomical_twilight_begin: 10:31:51
astronomical_twilight_begin: 1520821911
astronomical_twilight_end: 01:05:43
astronomical_twilight_end: 1520874343