Conor Mackel
Conor Mackel

Reputation: 31

Using date variables in an api

I have created a page that asks the user for a date and outputs results from an api. The api requires a start date and end date that can be no more than 7days apart. I have set it up so a user can enter a date and the end date will automatically be set for 7 days later.

I am having issues using the date function, It appears now that the code will automatically use todays date before the user can input their choose.

I want the user to be able to choose there date whether it be todays or a future date, I want my api call to wait for users input but not sure how this can be done.

    <?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>";
    }

    }
    ?> 

Upvotes: 1

Views: 210

Answers (1)

Jim
Jim

Reputation: 3619

It is nearly at what you wanted to begin with. Just need to add an else to the if statement and update the start/end dates to return false when no date is entered. Note: I also moved the header above the if and added a tertiary condition to display the date if it is already entered so that it is always displayed.

<?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

}

Upvotes: 1

Related Questions