我创建了一个页面,要求用户提供日期并输出api的结果。 api要求开始日期和结束日期相隔不超过7天。我已对其进行了设置,以便用户可以输入日期,结束日期将在7天后自动设置。
我在使用日期功能时遇到问题,现在看来代码会在用户输入他们的选择之前自动使用今天的日期。
我希望用户能够选择日期,无论是今天还是将来的日期,我希望我的api调用等待用户输入,但不知道如何做到这一点。
<?php
$startDate = date('Y-m-d', strtotime(isset($_GET['start'])? $_GET['start'] :date('Y-m-d')));
$endDate = date('Y-m-d', strtotime('+7 days', strtotime($startDate)));
if($startDate){
echo "$endDate";
$params = array(
'start_date' => $startDate,
'end_date' => $endDate,
'api_key' => 'coXJeNygdeuxVKs9yJLecWbfuXsY54Wi9gq37HuN'
);
$data = json_decode(callAPI('GET', 'https://api.nasa.gov/neo/rest/v1/feed', $params));
echo "<h1>Near-Earth Object (NEO) Report between " . $params['start_date'] . " and " . $params['end_date'] . "</h1>";
foreach ($data->near_earth_objects as $date => $count) {
echo "<p>" . sizeof($count) . " objects detected on $date</p>";
echo "<ol>";
foreach ($data->near_earth_objects->$date as $near_earth_object) {
echo "<li>" . $near_earth_object->name . " <a href='" . $near_earth_object->nasa_jpl_url . "'>" . $near_earth_object->nasa_jpl_url . "</a><br>";
echo "Estimated Diameter: " . $near_earth_object->estimated_diameter->meters->estimated_diameter_min . "-" . $near_earth_object->estimated_diameter->meters->estimated_diameter_max . " metres<br>";
echo "<ul>";
foreach ($near_earth_object->close_approach_data as $close_approach) {
echo "<li>Close approach on " . $close_approach->close_approach_date . " velocity " . $close_approach->relative_velocity->kilometers_per_hour . " km/h " . "missing " . $close_approach->orbiting_body . " by " . $close_approach->miss_distance->kilometers . " km</li> ";
}
echo "</ul></li>";
}
echo "</ol>";
}
}
?>
答案 0 :(得分:1)
这几乎就是你想要的开始。只需要在if语句中添加else,并在没有输入日期时更新开始/结束日期以返回false。注意:我还将标题移到if上方并添加了第三个条件以显示日期(如果已经输入),以便始终显示日期。
<?php
$startDate = isset($_GET['start']) ? date('Y-m-d', strtotime($_GET['start'] )) : false;
$endDate = $startDate ? date('Y-m-d', strtotime('+7 days', strtotime($startDate))) : false;
$params = array(
'start_date' => $startDate,
'end_date' => $endDate,
'api_key' => 'coXJeNygdeuxVKs9yJLecWbfuXsY54Wi9gq37HuN'
);
echo '<h1>Near-Earth Object (NEO) Report',
( $startDate ? ' between ' . $params['start_date'] . ' and ' . $params['end_date'] . '</h1>' : '</h1>');
if($startDate) {
echo "$endDate";
$data = json_decode(callAPI('GET', 'https://api.nasa.gov/neo/rest/v1/feed', $params));
foreach ($data->near_earth_objects as $date => $count) {
echo "<p>" . sizeof($count) . " objects detected on $date</p>";
echo "<ol>";
foreach ($data->near_earth_objects->$date as $near_earth_object) {
echo "<li>" . $near_earth_object->name . " <a href='" . $near_earth_object->nasa_jpl_url . "'>" . $near_earth_object->nasa_jpl_url . "</a><br>";
echo "Estimated Diameter: " . $near_earth_object->estimated_diameter->meters->estimated_diameter_min . "-" . $near_earth_object->estimated_diameter->meters->estimated_diameter_max . " metres<br>";
echo "<ul>";
foreach ($near_earth_object->close_approach_data as $close_approach) {
echo "<li>Close approach on " . $close_approach->close_approach_date . " velocity " . $close_approach->relative_velocity->kilometers_per_hour . " km/h " . "missing " . $close_approach->orbiting_body . " by " . $close_approach->miss_distance->kilometers . " km</li> ";
}
echo "</ul></li>";
}
echo "</ol>";
}
} else {
?><form action="" method="GET">
<label for="startdate">Please enter a start date (end date will be 7 days after the start date):</label>
<input id="startdate" type="date" name="start" />
<input type="submit" />
</form><?php
}