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.
答案 0 :(得分:1)
使用PHP xpath应该可以解决问题:
$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";
答案 1 :(得分:0)
你可以在这里查看时间;
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;
}
您还可以查看此溢出帖子以进行日期比较; PHP: How to check if a date is today, yesterday or tomorrow