1

I am trying to pull the weather from the current date from a weather xml file (http://www.yr.no/place/Ireland/Leinster/Dublin/forecast.xml). This xml file splits the weather from each date according to time (00:00-06:00, 06:00-12:00, etc).

So far I have been able to pull out the details I need for all weather for all dates using this code:

$urlweather = ("http://www.yr.no/place/Ireland/Leinster/Dublin/forecast.xml");
    $dublin_weather = simplexml_load_file($urlweather);

    echo "<table id='weather'>
            <tr>
            <th>Temperature</th>
            <th>Time / Date</th>
            <th>Forecast</th>
            </tr>";

    foreach ($dublin_weather->forecast->tabular->time as $liveweather){

        echo ("
        <tr>
            <td>" .$liveweather->temperature["value"]." °C ". "</td>
            <td>" .date_format(date_create($liveweather["from"]), "H:i") . 
            " - " .date_format(date_create($liveweather["to"]), "H:i"). " / " .date_format(date_create($liveweather["from"]), "d-m-y"). "</td>
            <td>" .$liveweather->symbol["name"]."</td>
        </tr>");
            //}
                                                        }
        echo "</table";

I would like to be able to only output the weather for the current date so that the weather gets updated every day based on that days date, but cannot seem to figure it out.

Any help would be appreciated.

2 Answers2

1

Using PHP xpath should do the trick:

$urlweather = ("http://www.yr.no/place/Ireland/Leinster/Dublin/forecast.xml");
$dublin_weather = simplexml_load_file($urlweather);

$matchesObj = $dublin_weather->xpath("forecast//tabular//time[contains(@from, '".date('Y-m-d',time())."')]"); // search attributes containing today's date


echo "<table id='weather'>
        <tr>
        <th>Temperature</th>
        <th>Time / Date</th>
        <th>Forecast</th>
        </tr>";

foreach ($matchesObj as $liveweather){

    echo ("
    <tr>
        <td>" .$liveweather->temperature["value"]." °C ". "</td>
        <td>" .date_format(date_create($liveweather["from"]), "H:i") . 
        " - " .date_format(date_create($liveweather["to"]), "H:i"). " / " .date_format(date_create($liveweather["from"]), "d-m-y"). "</td>
        <td>" .$liveweather->symbol["name"]."</td>
    </tr>");
        //}

}

echo "</table";
Yolo
  • 1,569
  • 1
  • 11
  • 16
0

You can just check time here;

foreach ($dublin_weather->forecast->tabular->time as $liveweather) {
  // insert condition check here
  // if ($diffDays === 0) :
  // (or something that compares weather date today..etc)
    echo ("
      <tr>
        <td>" .$liveweather->temperature["value"]." °C ". "</td>
        <td>" .date_format(date_create($liveweather["from"]), "H:i") . 
            " - " .date_format(date_create($liveweather["to"]), "H:i"). " / " .date_format(date_create($liveweather["from"]), "d-m-y"). "</td>
        <td>" .$liveweather->symbol["name"]."</td>
      </tr>");
  //endif; 
}

You may also look at this overflow post for date comparing; PHP: How to check if a date is today, yesterday or tomorrow

Community
  • 1
  • 1
Perspective
  • 642
  • 5
  • 12