Extract json information in php

-1

Personal I have following code:

<?php

$key = "*****************";
$forcast_days='5';
$city = '-30.1087957,-51.3169879';
$url ="http://api.apixu.com/v1/forecast.json?key=$key&q=$city&days=$forcast_days&=";

$ch = curl_init();  
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

$json_output=curl_exec($ch);
$weather = json_decode($json_output);


$days = $weather->forecast->forecastday;

echo "Cidade: ". $weather->location->name;
echo "<br>";
echo "Estado: ".$weather->location->region;
echo "<br>";
echo "Pais: ".$weather->location->country;

foreach ($days as $day){


echo "<table>";    
    echo "<tr><td colspan='4' border='0'><h2>{$day->date}</h2>";

    echo "<tr><td><h4>Wind</h4></td><td colspan='3'>{$day->day->maxwind_mph}Mph <br> {$day->day->maxwind_kph}kph </td></tr>";
    foreach ($day->hour as $hr){

        echo "<tr><td colspan='4' border='0'>";
        echo "<table style='width:100%;'>";    

        echo "<tr><td>Time</td><td>Temprature</td><td>Wind</td><td>Humidity</td></tr>";
        echo "<tr><td><div>{$hr->time}<img src=' {$hr->condition->icon}'/></div></td><td>{$hr->temp_c}&deg;C<br>{$hr->temp_f}&deg;F</td><td>{$hr->wind_mph}Mph <br> {$hr->wind_kph}kph</td><td>$hr->humidity</td></tr>";

        echo "</table></tr></td>";
    }
echo "</table> <br>";        

Here is the result on the web: link

How can I select variables, for example, from 00:00 to 06:00 only?

    
asked by anonymous 02.01.2017 / 12:52

1 answer

1

This would work:

<?php

$key = '***************************';
$forcast_days='5';
$city = '-30.1087957,-51.3169879';
$url ="http://api.apixu.com/v1/forecast.json?key=$key&q=$city&days=$forcast_days&=";

$ch = curl_init();  
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

$json_output=curl_exec($ch);
$weather = json_decode($json_output);

$days = $weather->forecast->forecastday;

foreach($days as $day) {
    echo "<h1>".date('d/m/Y', strtotime($day->date))."</h1>";

    foreach($day->hour as $hour) {
        $time = explode(' ', $hour->time);
        $time = $time[1];
        $time = explode(':', $time);
        $time = $time[0];

        if($time >= 0 and $time <= 6) {
            echo "As {$time}:00h teremos {$hour->temp_c}Cº<br>";
        }
    }
    echo '<hr>';
}

In the return, I took the days that were selected, I went through this list of days, each element showed a title with the date of the day, and within that day I went through its hours, in return I noticed that $hour->time would give a date and time in the format 2017-01-02 00:00 so I cut that string in space () and then the colon (:), so the result was the time of day, so it was easy to do the comparison. >     

02.01.2017 / 14:58