Cómo encontrar elementos nil a través de XML XPATH a través de PHP
Pregunta
Tengo algunos elementos que son nulos. Los elementos devueltos están en una marca de tiempo y quiero convertir todos los elementos nil a una fecha actual usando getdate()
. Lo siento si no estoy usando la terminología correcta o haciendo las preguntas correctas, no dude en editar si lo entiende.
Aquí está 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>
El PHP:
$xml = new SimpleXMLElement($result);
$solved_at = $xml->xpath('ticket/solved-at');
... array
echo "Ticket time solved at:" . $solved_at[$i]. "<br/>";
El retorno:
Ticket time solved at:2012-02-20T17:58:34-5:00<br/>
Ticket time solved at:
Me gustaría devolver el DateTime actual en UNIX para las cantidades nulas en el XML, ¿alguna idea? He probado lo siguiente, pero en vano, obtengo los mismos resultados que anteriormente:
if (is_null($solved_at[$i])) {
$solved_at = getdate(); }
Solución
Prueba esto.
$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/>";
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow