百科狗-知识改变命运!
--

date_sun_info() - php 日期时间函数

梵高1年前 (2023-11-21)阅读数 24#技术干货
文章标签例程

date_sun_info()

(PHP 5 >= 5.1.2, PHP 7)

Returns an array with information about sunset/sunrise and twilight begin/end

说明

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

参数

$time

Timestamp.

$latitude

date_sun_info() - php 日期时间函数

Latitude in degrees.

$longitude

Longitude in degrees.

返回值

Returns array on success 或者在失败时返回FALSE. The structure of the array is detailed in the following list:

sunriseThe time of the sunrise (zenith angle = 90°35').sunsetThe time of the sunset (zenith angle = 90°35').transitThe time when the sun is at its zenith, i.e. has reached its topmost point.civil_twilight_beginThe start of the civil dawn (zenith angle = 96°). It ends atsunrise.civil_twilight_endThe end of the civil dusk (zenith angle = 96°). It starts atsunset.nautical_twilight_beginThe start of the nautical dawn (zenith angle = 102°). It ends atcivil_twilight_begin.nautical_twilight_endThe end of the nautical dusk (zenith angle = 102°). It starts atcivil_twilight_end.astronomical_twilight_beginThe start of the astronomical dawn (zenith angle = 108°). It ends atnautical_twilight_begin.astronomical_twilight_endThe end of the astronomical dusk (zenith angle = 108°). It starts atnautical_twilight_end.

The values of the array elements are either UNIX timestamps,FALSEif the sun is below the respective zenith for the whole day,orTRUEif the sun is above the respective zenith for the whole day.

更新日志

版本说明
5.2.2The order of$latitudeand$longitudehas been swapped.

范例

Adate_sun_info()example

以上例程会输出:

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

Polar night

以上例程会输出:

array(9) {
  ["sunrise"]=>
  bool(false)
  ["sunset"]=>
  bool(false)
  ["transit"]=>
  int(1513857490)
  ["civil_twilight_begin"]=>
  bool(false)
  ["civil_twilight_end"]=>
  bool(false)
  ["nautical_twilight_begin"]=>
  bool(false)
  ["nautical_twilight_end"]=>
  bool(false)
  ["astronomical_twilight_begin"]=>
  bool(false)
  ["astronomical_twilight_end"]=>
  bool(false)
}

Midnight sun

以上例程会输出:

array(9) {
  ["sunrise"]=>
  bool(true)
  ["sunset"]=>
  bool(true)
  ["transit"]=>
  int(1498046510)
  ["civil_twilight_begin"]=>
  bool(true)
  ["civil_twilight_end"]=>
  bool(true)
  ["nautical_twilight_begin"]=>
  bool(true)
  ["nautical_twilight_end"]=>
  bool(true)
  ["astronomical_twilight_begin"]=>
  bool(true)
  ["astronomical_twilight_end"]=>
  bool(true)
}

参见

  • date_sunrise() 返回给定的日期与地点的日出时间
  • date_sunset() 返回给定的日期与地点的日落时间
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. 
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
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:

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] 
* * *

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)