【问题标题】:Display data from XML file from a specific date using PHP使用 PHP 显示来自特定日期的 XML 文件中的数据
【发布时间】:2017-03-02 23:28:40
【问题描述】:

我正在尝试从天气 xml 文件 (http://www.yr.no/place/Ireland/Leinster/Dublin/forecast.xml) 中提取当前日期的天气。此 xml 文件根据时间(00:00-06:00、06:00-12:00 等)从每个日期拆分天气。

到目前为止,我已经能够使用此代码提取所有日期的所有天气所需的详细信息:

$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";

我希望能够只输出当前日期的天气,以便根据该日期每天更新天气,但似乎无法弄清楚。

任何帮助将不胜感激。

【问题讨论】:

标签: php xml


【解决方案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";

【讨论】:

  • 效果很好。谢谢你。仍在学习如何在 PHP 中处理 XML 文件,因此将更多地研究 Xpath
【解决方案2】:

您可以在这里查看时间;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多