01.xml
-------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<message>
<record>
<id>1</id>
<name>01.jpg</name>
<showtime>08:00:00-09:59:00</showtime>
</record>
<record>
<id>2</id>
<name>02.jpg</name>
<showtime>10:00:00-10:20:00</showtime>
</record>
<record>
<id>3</id>
<name>03.jpg</name>
<showtime>10:28:00-19:00:00</showtime>
</record>
</message>
show_pic.php(我的代码)
--------------------------------------------------------------------------------------------------
<?
$xmlDoc = new DOMDocument(); //创建一个DOMDocument对象
$xmlDoc->load("01.xml"); //调用该类的load()方法,载入xml文件
$xpath = new DOMXPath($xmlDoc); //创建一个DOMXPath对象 DOMXPath()是XML的SQL语句
$pic_time = $xpath->query("//showtime"); //读取showtime节点
foreach ($pic_time as $node){
$node_time[$k] = $node->textContent;
$arr_node_text = explode("-",$node_time[$k]); //拆分,原格式为08:00:00-09:00:00
$arr_stime = explode(":",$arr_node_text[0]);//再拆时间段起点
$arr_etime = explode(":",$arr_node_text[1]);//再拆时间段终点
$sys_time = date("H:i:s"); //获取系统时间
$arr_sys_time = explode(":",$sys_time);//再拆
//计算时间差值,所得到的值为从1970-1-1 00:00:00 GMT到参数时间的总秒数:是整数
$s1=mktime($arr_sys_time[0],$arr_sys_time[1],$arr_sys_time[2]); //系统时间相距1970-1-1 00:00:00 GMT的秒数
$s2=mktime($arr_stime[0],$arr_stime[1],$arr_stime[2]); //时间段起点,相距1970-1-1 00:00:00 GMT的秒数
$s3=mktime($arr_etime[0],$arr_etime[1],$arr_etime[2]); //时间段终点,相距1970-1-1 00:00:00 GMT的秒数
if($s2-$s1<=0 && $s1-$s3<=0){
$pic_name = $xpath->query("//record[showtime='$node_time[$k]']");
$g=0;
foreach ($pic_name->item(0)->childNodes as $r_node){
$arr_r[$g] = $r_node->textContent;
echo "/$arr_r[$g] = ".$arr_r[$g]."<br>";
$g++;
}
echo <<<EOD
<div><img src="images/{$arr_r[3]}" border=0></div> <!-- 显示图片 -->
EOD;
}
$k++;
}
?>
show_pic2.php(别人的代码)
--------------------------------------------------------------------------------------------------------
<?
$xmlDoc = new DOMDocument(); //创建一个DOMDocument对象
$xmlDoc->load("01.xml"); //调用该类的load()方法,载入xml文件
$xpath = new DOMXPath($xmlDoc); //创建一个DOMXPath对象 DOMXPath()是XML的SQL语句
$pic_time = $xpath->query("//showtime"); //读取showtime节点
$k=0;
$nnn=time();
foreach ($pic_time as $node)
{
$ttime=explode("-",$node->textContent);
$stime = strtotime($ttime[0]);
$stime2= strtotime($ttime[1]);
if($nnn>=$stime and $nnn<=$stime2){
echo $node->parentNode->childNodes->item(3)->nodeValue;
}
}
?>
-------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<message>
<record>
<id>1</id>
<name>01.jpg</name>
<showtime>08:00:00-09:59:00</showtime>
</record>
<record>
<id>2</id>
<name>02.jpg</name>
<showtime>10:00:00-10:20:00</showtime>
</record>
<record>
<id>3</id>
<name>03.jpg</name>
<showtime>10:28:00-19:00:00</showtime>
</record>
</message>
show_pic.php(我的代码)
--------------------------------------------------------------------------------------------------
<?
$xmlDoc = new DOMDocument(); //创建一个DOMDocument对象
$xmlDoc->load("01.xml"); //调用该类的load()方法,载入xml文件
$xpath = new DOMXPath($xmlDoc); //创建一个DOMXPath对象 DOMXPath()是XML的SQL语句
$pic_time = $xpath->query("//showtime"); //读取showtime节点
foreach ($pic_time as $node){
$node_time[$k] = $node->textContent;
$arr_node_text = explode("-",$node_time[$k]); //拆分,原格式为08:00:00-09:00:00
$arr_stime = explode(":",$arr_node_text[0]);//再拆时间段起点
$arr_etime = explode(":",$arr_node_text[1]);//再拆时间段终点
$sys_time = date("H:i:s"); //获取系统时间
$arr_sys_time = explode(":",$sys_time);//再拆
//计算时间差值,所得到的值为从1970-1-1 00:00:00 GMT到参数时间的总秒数:是整数
$s1=mktime($arr_sys_time[0],$arr_sys_time[1],$arr_sys_time[2]); //系统时间相距1970-1-1 00:00:00 GMT的秒数
$s2=mktime($arr_stime[0],$arr_stime[1],$arr_stime[2]); //时间段起点,相距1970-1-1 00:00:00 GMT的秒数
$s3=mktime($arr_etime[0],$arr_etime[1],$arr_etime[2]); //时间段终点,相距1970-1-1 00:00:00 GMT的秒数
if($s2-$s1<=0 && $s1-$s3<=0){
$pic_name = $xpath->query("//record[showtime='$node_time[$k]']");
$g=0;
foreach ($pic_name->item(0)->childNodes as $r_node){
$arr_r[$g] = $r_node->textContent;
echo "/$arr_r[$g] = ".$arr_r[$g]."<br>";
$g++;
}
echo <<<EOD
<div><img src="images/{$arr_r[3]}" border=0></div> <!-- 显示图片 -->
EOD;
}
$k++;
}
?>
show_pic2.php(别人的代码)
--------------------------------------------------------------------------------------------------------
<?
$xmlDoc = new DOMDocument(); //创建一个DOMDocument对象
$xmlDoc->load("01.xml"); //调用该类的load()方法,载入xml文件
$xpath = new DOMXPath($xmlDoc); //创建一个DOMXPath对象 DOMXPath()是XML的SQL语句
$pic_time = $xpath->query("//showtime"); //读取showtime节点
$k=0;
$nnn=time();
foreach ($pic_time as $node)
{
$ttime=explode("-",$node->textContent);
$stime = strtotime($ttime[0]);
$stime2= strtotime($ttime[1]);
if($nnn>=$stime and $nnn<=$stime2){
echo $node->parentNode->childNodes->item(3)->nodeValue;
}
}
?>