- simplexml_load_string: documentation ( source)
- strtotime: documentation ( source)
- usort: documentation ( source)
<?php
$xml=simplexml_load_string(<<<XML
<?xml version="1.0"?>
<calender>
<item>
<date>20140113</date>
<title>My Birthday</title>
</item>
<item>
<date>20140112</date>
<title>test</title>
</item>
<item>
<date>20130114</date>
<title>test</title>
</item>
</calender>
XML
);
$arr=array();
foreach($xml->item as $aTask)
{
$arr[]=$aTask;
}
//print_r($arr);
usort($arr,function($a,$b){
return strtotime($a->date)-strtotime($b->date);
});
//print_r($arr);
$xml=simplexml_load_string(<<<XML
<?xml version="1.0"?>
<calender>
</calender>
XML
);
foreach($arr as $aTask)
{
$tTask=$xml->addChild($aTask->getName());
$tTask->addChild($aTask->date->getName(),(string)$aTask->date);
$tTask->addChild($aTask->title->getName(),(string)$aTask->title);
}
echo $xml->asXML();