我一直在尝试列出上学和不上学的日子.
<?PHP
$fecha1 = "2015-03-10";
$fecha2 = date("Y-m-d",strtotime($fecha1."+ 10 days"));
$fecha3 = array("2015-03-11","2015-03-14","2015-03-17");
$j=1;
for($i=$fecha1;$i<$fecha2;$i = date("Y-m-d", strtotime($i ."+ 1 days"))){
for ($n=0; $n <count($fecha3) ; $n++) {
if($i==$fecha3[$n]){
$obs="not there";
}else{
$obs="there";
}
}
echo "Day ".$j." ".$i."---".$obs."<br />";
$j++;
}
?>
输出是
Day 1 2015-03-10---there
Day 2 2015-03-11---there
Day 3 2015-03-12---there
Day 4 2015-03-13---there
Day 5 2015-03-14---there
Day 6 2015-03-15---there
Day 7 2015-03-16---there
Day 8 2015-03-17---not there
Day 9 2015-03-18---there
Day 10 2015-03-19---there
我不明白为什么它在第二天不说“不在那里” 2015-03-11
2015年5月5日,第5天,有人帮助我,我已经使用了几个小时.
解决方法:
if($i==$fecha3[$n]){
$obs="not there";
break; // this is important
}else{
$obs="there";
}
另一种选择是in_array(),用于搜索:
if(in_array($i, $fecha3)){
$obs="not there";
}else{
$obs="there";
}