我有一个需要从中提取数据的 XML 文档。我基本上是在制定一个时间表。我已经有一个变量 $timeToFind,我从“7:00am”到“10:00pm”增加了半小时。我还有一种方法来计算事件的时间长度。
如果在星期一(星期一)发现任何事件,我需要使用某种方法在 $timeToFind 处检查。如果找到,则回显一个包含时间和类型的 div,否则,回显一个空 div。
$xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?>
<schedule>
<about>
<name>Person 1</name>
</about>
<daily>
<mon>
<event type="work" start="8:00am" end="12:00pm">
<title>Work at McDonalds</title>
<description>Go to work and get free food.</description>
</event>
<event type="classs" start="12:30pm" end="2:30pm">
<title>English Class</title>
<description>Go to English class in Room 219.</description>
</event>
</mon>
</daily>
</schedule>');
$dayStart = 7;
$dayEnd = 22.5;
$hoursInDay = $dayEnd - $dayStart;
for ($i=$dayStart; $i<=$dayEnd; $i+=.5) {
// if it happens to be 1:00 (13), keep the $current variable non-military
if($i >= 13){
$current = $i-12;
}else {
$current = $i;
}
// $i >= 12, then it's PM, else AM
if($i >= 12){
$timeToFind = $timeToFind . "pm";
}else {
$timeToFind = $timeToFind . "am";
}
$nodes = $xml->xpath(sprintf('/mon/event[@type="%s"]', $timeToFind));
if (!empty($nodes)) {
printf('Time "%s" found!! <br> ', $timeToFind);
} else {
printf('Time "%s" Not found. <br> ', $timeToFind);
}
}