How to find nil elements through xml xpath via php
Frage
I have some elements that are nil. The returned elements are in a timestamp and I want to turn all the nil elements to a current date using getdate()
. I'm sorry if im not using the right terminology or asking the right questions, feel free to edit if you understand.
Here is XML:
<tickets type="array" count="2">
<ticket>
<solved-at type="datetime">2012-02-20T17:58:34-05:00</solved-at>
</ticket>
<ticket>
<solved-at type="datetime" nil="true"/>
</ticket>
</tickets>
The php:
$xml = new SimpleXMLElement($result);
$solved_at = $xml->xpath('ticket/solved-at');
... array
echo "Ticket time solved at:" . $solved_at[$i]. "<br/>";
The returned:
Ticket time solved at:2012-02-20T17:58:34-5:00<br/>
Ticket time solved at:
I'd like to return the current datetime in UNIX for the null amounts in the xml, any ideas? I have tried the below but to no avail, I get same results as above:
if (is_null($solved_at[$i])) {
$solved_at = getdate(); }
Lösung
Try this.
$xml = simplexml_load_file("solved.xml");
$solved_at = $xml->xpath('ticket/solved-at');
foreach($solved_at as $solved) {
if ($solved['nil']) {
$r = date('Y-m-d h:i:s a', time()); //getdate();
}
echo "Ticket time solved at:" . $r. "<br/>";
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow