Filtering XML file, with PHP

前端 未结 2 520
一整个雨季
一整个雨季 2021-01-16 01:53

I want to load XML file and then remove all where is bigger/older then 7 years. Date format is YYYY-MM-DD.

2条回答
  •  长情又很酷
    2021-01-16 02:09

    Here are some tips on how to get started:

    1. You need to parse the XML to something a little easier to work with. PHP has a library called SimpleXML.
    2. Loop through the data and remove the objects which are older than 7 years. To compare dates you have to first convert the dates you got from the XML to something PHP can process as a date. Take a look at strtotime which gives you the timestamp (seconds since 1970, actually 1901 for version > 5.1.0) or DateTime which supports dates before 1970.
    3. To check if the fetched date is older than 7 years ago, you need to (one way) subtract the timestamp with the current timestamp, and see if that value is greater than 7 years in seconds. Or if you use DateTime, you can take a look at DateTime::diff. Remove the objects that you iterate over that are older than 7 years (unset).
    4. To save as XML again, take a look at SimpleXMLElement::asXML

    Hope that helps!

提交回复
热议问题