【发布时间】:2020-09-23 09:55:28
【问题描述】:
我已经在 SO 上讨论了多个问题和答案,但我似乎无法弄清楚。我实际上是在尝试检查某个时间某个地点(纬度/经度)是否是夜晚。
到目前为止,我设法获得了特定位置的日出和日落时间,然后我试图将其与当前时间进行比较。我有一点灰姑娘的情况,它可以正常工作到午夜来袭。
我正在创建我的变量进行比较,如下所示:
$currenttime = DateTime::createFromFormat('Y-m-d H:i', $templooptime);
$sunsettime = DateTime::createFromFormat('Y-m-d H:i', $sunsetfinal);
$sunrisetime = DateTime::createFromFormat('Y-m-d H:i', $sunrisefinal);
我正在做这样一个简单的 IF 语句:
if ($currenttime>$sunsettime) {
$nightminutes++;
}
这会输出以下内容:
Current Time:2020-09-22 23:58:00| Location: 50.1776906598,-51.077228098126 | Sunset Start: 2020-09-22 18:48:00 |Sunrise Start: 2020-09-22 06:45:00 | Night:287
Current Time:2020-09-22 23:59:00| Location: 50.202093865458,-50.984342445684 | Sunset Start: 2020-09-22 18:47:00 |Sunrise Start: 2020-09-22 06:44:00 | Night:288
Current Time:2020-09-23 00:00:00| Location: 50.226422634856,-50.891362177619 | Sunset Start: 2020-09-23 18:45:00 |Sunrise Start: 2020-09-23 06:46:00 | Night:0
Current Time:2020-09-23 00:01:00| Location: 50.25067682914,-50.798287405434 | Sunset Start: 2020-09-23 18:44:00 |Sunrise Start: 2020-09-23 06:45:00 | Night:0
当我将逻辑更改为:
if ($currenttime>$sunsettime && $currenttime<$sunrisetime) {
$nightminutes++;
}
它输出:
Current Time:2020-09-22 23:58:00| Location: 50.1776906598,-51.077228098126 | Sunset Start: 2020-09-22 18:48:00 |Sunrise Start: 2020-09-22 06:45:00 | Night:0
Current Time:2020-09-22 23:59:00| Location: 50.202093865458,-50.984342445684 | Sunset Start: 2020-09-22 18:47:00 |Sunrise Start: 2020-09-22 06:44:00 | Night:0
Current Time:2020-09-23 00:00:00| Location: 50.226422634856,-50.891362177619 | Sunset Start: 2020-09-23 18:45:00 |Sunrise Start: 2020-09-23 06:46:00 | Night:0
Current Time:2020-09-23 00:01:00| Location: 50.25067682914,-50.798287405434 | Sunset Start: 2020-09-23 18:44:00 |Sunrise Start: 2020-09-23 06:45:00 | Night:0
我该如何解决这个问题?
更新根据 RO 的回答将日落时间设置为提前一天,仍然存在这个问题:
Current Time:2020-09-22 23:59:00 | Location: 50.202093865458,-50.984342445684 | Sunset Start: 2020-09-22 18:47:00 | Sunrise Start: 2020-09-23 06:46:00 | Night:288
Current Time:2020-09-23 00:00:00 | Location: 50.226422634856,-50.891362177619 | Sunset Start: 2020-09-23 18:45:00 | Sunrise Start: 2020-09-24 06:47:00 | Night:0
当它超过午夜时,日落时间比当前时间少,因此它显示为 false。有什么想法吗?
UPDATE 2这是我正在使用的基本代码(摘录):
<?php
$fulltimestampdateforloop = new DateTime('2020-09-22 23:58:00');
$coords=["50.1776906598,-51.077228098126","50.202093865458,-50.984342445684","50.226422634856,-50.891362177619","50.25067682914,-50.798287405434"];
$nightmin=0;
$offset2=2;
foreach ($coords as $value) {
$latlng = explode(",", $value);
$lat = $latlng[0];
$lng = $latlng[1];
$str_timestamp = $fulltimestampdateforloop->format('Y-m-d');
$str_flighttime =$fulltimestampdateforloop->getTimestamp();
$sunsetTime_a = (clone $fulltimestampdateforloop);
$sunriseTime_a = (clone $fulltimestampdateforloop)->add(new DateInterval('P1D'));
$sunsetStr = date_sunset($sunsetTime_a->getTimestamp(), SUNFUNCS_RET_STRING, $lat, $lng, 90,$offset2);
$sunriseStr = date_sunrise($sunriseTime_a->getTimestamp(), SUNFUNCS_RET_STRING, $lat, $lng, 90,$offset2);
$ssettime = explode(":", $sunsetStr);
$sunsetStr_h = $ssettime[0];
$sunsetStr_m = $ssettime[1];
$srisetime = explode(":", $sunriseStr);
$sunriseStr_h = $srisetime[0];
$sunriseStr_m = $srisetime[1];
$sunsetTime_e = (clone $fulltimestampdateforloop)->setTime($sunsetStr_h,$sunsetStr_m);
$sunriseTime_e = (clone $fulltimestampdateforloop)->add(new DateInterval('P1D'))->setTime($sunriseStr_h,$sunriseStr_m);
if ($fulltimestampdateforloop > $sunsetTime_e && $fulltimestampdateforloop < $sunriseTime_e) {
$nightmin="Night";
} else{
$nightmin="Day";
}
echo 'Current Time:'.$fulltimestampdateforloop->format('Y-m-d H:i:s').' | Location: '.$lat.','.$lng.' | Sunset Start: '.$sunsetTime_e->format('Y-m-d H:i:s').' | Sunrise Start: '.$sunriseTime_e->format('Y-m-d H:i:s').' | Night or Day? : '.$nightmin."<br>";
$fulltimestampdateforloop->modify('+1 minutes');
}
?>
【问题讨论】: