如何区分日期列表中的日期?PHP

如何区分日期列表中的日期?PHP,php,arrays,date,Php,Arrays,Date,我一直在试着列出我上学和没上学的日子 我在这里过日子。另一个数组包含我没有上学的日子 <?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", strtot

我一直在试着列出我上学和没上学的日子

我在这里过日子。另一个数组包含我没有上学的日子

<?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++;
}
?>
我不明白为什么它在第2天没有说“不在那里”
2015-03-11

第5天
2015-03-14
,请有人帮帮我,我已经做了好几个小时了。

一旦找到针,你应该添加一个
断针

if($i==$fecha3[$n]){
        $obs="not there";
        break; // this is important
    }else{
        $obs="there";
    }
另一种选择是在_array()中进行搜索:

if(in_array($i, $fecha3)){
    $obs="not there";
}else{
    $obs="there";
}

这是因为
2015-03-11
2015-03-14
$fecha3
数组中的前两个值,
$obs
在第二个for循环中被覆盖

在这种情况下,我建议使用,而不是第二个for循环:

$fecha1 = '2015-03-10';
$fecha2 = 10;
$fecha3 = array('2015-03-11', '2015-03-14', '2015-03-17');

for ($i = 0; $i < $fecha2; $i++) {
    $date = date('Y-m-d', strtotime($fecha1 . ' + ' . $i . ' days'));
    $obs = in_array($date, $fecha3) ? 'not there' : 'there';
    echo 'Day ' . ($i + 1) . ' ' . $date . '---' . $obs . '<br />';
}
$fecha1='2015-03-10';
$fecha2=10;
$fecha3=阵列('2015-03-11','2015-03-14','2015-03-17');
对于($i=0;$i<$fecha2;$i++){
$date=date('Y-m-d',strottime($fecha1.'+'.$i.'days');
$obs=in_数组($date,$fecha3)?“不存在”:“存在”;
回显'Day'($i+1)。'.$date.'-'.$obs.
; }
由于您的
for
循环迭代了所有
$fecha3
项,因此您实际上是在将每个日期与
2015-03-17
(数组中的最后一项)进行比较。使用@Ghost-answer@RildoGomez是的,您正在迭代
$fecha3
中的每个元素,即使已经找到了指针,您也应该在该点停止。很高兴这有助于汉克斯的解释,我正要问:)
$fecha1 = '2015-03-10';
$fecha2 = 10;
$fecha3 = array('2015-03-11', '2015-03-14', '2015-03-17');

for ($i = 0; $i < $fecha2; $i++) {
    $date = date('Y-m-d', strtotime($fecha1 . ' + ' . $i . ' days'));
    $obs = in_array($date, $fecha3) ? 'not there' : 'there';
    echo 'Day ' . ($i + 1) . ' ' . $date . '---' . $obs . '<br />';
}