PHP date_sun_info() 函数



定义和用法

date_sun_info() 函数接受时间、地点的纬度和经度,并提供有关该地点日出/日落以及暮光开始/结束的信息。

语法

date_sun_info($timestamp, $latitude, $longitude)

参数

序号 参数及描述
1

timestamp (必填)

指定一个时间戳。

2

latitude (必填)

指定一个地点的纬度。

3

longitude (必填)

指定一个地点的经度。

返回值

PHP date_sun_info() 函数返回一个数组,其中包含指定地点在给定日期的日出/日落以及暮光开始/结束的信息。

PHP 版本

此函数首次引入于 PHP 5.2.0 版,并在所有后续版本中均有效。

示例

以下示例演示了 date_sun_info() 函数的用法 -

<?php
   $sun_info = date_sun_info("02-17-2012", 20.5937, 78.9629);
   print_r($sun_info);
?>

这将产生以下结果 -

Array
(
    [sunrise] => 4818
    [sunset] => 44087
    [transit] => 24453
    [civil_twilight_begin] => 3381
    [civil_twilight_end] => 45524
    [nautical_twilight_begin] => 1729
    [nautical_twilight_end] => 47176
    [astronomical_twilight_begin] => 98
    [astronomical_twilight_end] => 48807
)

示例

以下示例获取同一日期在不同地点的信息 -

<?php
   $sun_info = date_sun_info("02-17-2012", 37.0902, 95.7129);
   print_r($sun_info);
?>

这将产生以下结果 -

Array
(
    [sunrise] => 3038
    [sunset] => 37825
    [transit] => 20431
    [civil_twilight_begin] => 1307
    [civil_twilight_end] => 39556
    [nautical_twilight_begin] => -642
    [nautical_twilight_end] => 41505
    [astronomical_twilight_begin] => -2538
    [astronomical_twilight_end] => 43402
)

示例

以下示例获取不同日期在同一地点的信息 -

<?php
   $time = "2000-01-01";
   $latitude = 31.7667;
   $longitude = 35.2333;
   print_r(date_sun_info($time, $latitude, $longitude));
   $time = "2010-01-01";
   print_r(date_sun_info($time, $latitude, $longitude));   
   $time = "2020-01-01";
   print_r(date_sun_info($time, $latitude, $longitude));
?>

这将产生以下结果 -

Array
(
    [sunrise] => 16742
    [sunset] => 53161
    [transit] => 34951
    [civil_twilight_begin] => 15138
    [civil_twilight_end] => 54765
    [nautical_twilight_begin] => 13316
    [nautical_twilight_end] => 56587
    [astronomical_twilight_begin] => 11534
    [astronomical_twilight_end] => 58369
)
Array
(
    [sunrise] => 16742
    [sunset] => 53161
    [transit] => 34951
    [civil_twilight_begin] => 15138
    [civil_twilight_end] => 54765
    [nautical_twilight_begin] => 13316
    [nautical_twilight_end] => 56587
    [astronomical_twilight_begin] => 11534
    [astronomical_twilight_end] => 58369
)
Array
(
    [sunrise] => 16742
    [sunset] => 53161
    [transit] => 34951
    [civil_twilight_begin] => 15138
    [civil_twilight_end] => 54765
    [nautical_twilight_begin] => 13316
    [nautical_twilight_end] => 56587
    [astronomical_twilight_begin] => 11534
    [astronomical_twilight_end] => 58369
)

示例

<?php
   $sun_info = date_sun_info(strtotime("2017-07-12"), 20.5937, 78.9629);
   foreach ($sun_info as $key => $val) {
      echo "$key: " . date("H:i:s", $val) . "\n";
   }
?>

这将产生以下结果 -

sunrise: 00:11:03
sunset: 13:28:33
transit: 06:49:48
civil_twilight_begin: 23:46:45
civil_twilight_end: 13:52:51
nautical_twilight_begin: 23:17:48
nautical_twilight_end: 14:21:47
astronomical_twilight_begin: 22:47:55
astronomical_twilight_end: 14:51:41
php_function_reference.htm
广告