PHP允许我在特定纬度和经度的任何一天快速检查日出和夕阳时间。
有一个简单的方法来计算哪一天是冬至?我的意思是--在我的特定地点,哪一天的日照时间最多,哪一天的日照最少?
发布于 2014-06-01 10:15:05
我不认为这是“简单的”,但我考虑了计算每天日出和日落之间的时间差,然后将这些数据存储在一个数组中,最后找到min/max值。我做了一些非常快的事情,希望它能有用:
(我用的是随机长/拉)
function solstice() {
// Set timezone
date_default_timezone_set('UTC');
$date='2014/01/01';
$end_date='2014/12/31';
$i = 0;
//loop through the year
while(strtotime($date)<=strtotime($end_date)) {
$sunrise=date_sunrise(strtotime($date),SUNFUNCS_RET_DOUBLE,31.47,35.13,90,3);
$sunset=date_sunset(strtotime($date),SUNFUNCS_RET_DOUBLE,31.47,35.13,90,3);
//calculate time difference
$delta = $sunset-$sunrise;
//store the time difference
$delta_array[$i] = $delta;
//store the date
$dates_array[$i] = $date;
$i++;
//next day
$date=date("Y-m-d",strtotime("+1 day",strtotime($date)));
}
$shortest_key = array_search(min($delta_array), $delta_array);
$longest_key = array_search(max($delta_array), $delta_array);
echo "The longest day is:".$dates_array[$longest_key]. "<br />";
echo "The shortest day is:".$dates_array[$shortest_key]. "<br />";
}发布于 2021-12-27 02:44:51
上面的代码使用的函数将在PHP8.1中折旧,并且取决于系统配置,可能导致内存耗尽。我冒昧地重写了函数,因此它与PHP 8兼容。
事实上,我们知道夏至发生在6月20日至12月23日之间。考虑到这一点,我们能够把重点放在这两个具体的月和这四个具体的日子。
function solstice() {
$delta = $dates = array();
$year = date('Y', time());
$months = array(6,12);
$days = array(19,20,21,22,23,24);
$latitude = 31.47;
$longitude = 35.13;
$dateFormat = 'l, d F Y';
foreach ($months AS $month) {
foreach ($days AS $day) {
$date = sprintf('%d-%02d-%d', $year, $month, $day);
$solar = date_sun_info(strtotime($date), $latitude, $longitude);
$delta[] = ($solar['sunset'] - $solar['sunrise']);
$dates[] = $date;
unset($solar);
}
}
print('<pre>');
print_r($delta);
print_r($dates);
print('</pre>');
$shortest_key = array_search(min($delta), $delta);
$longest_key = array_search(max($delta), $delta);
printf(_('The longest day is: %s<br />'), date($dateFormat, strtotime($dates[$longest_key])));
printf(_('The shortest day is: %s'), date($dateFormat, strtotime($dates[$shortest_key])));
}感谢@MorKadosh播撒了基本思想。
https://stackoverflow.com/questions/23978449
复制相似问题