How to distinguish days between a list of dates? PHP
I tried to make a list of the days when I went to school and when I didn't.
I will loop the days here. Another array contains the days when I didn't go to school.
<?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++;
}
?>
and the output is
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
I don't understand why he doesn't say "no" on day 2 2015-03-11
and day 5 2015-03-14
, someone helps me, I was with this for hours.
source to share
This is because 2015-03-11
u 2015-03-14
are the first two values in the array $fecha3
, and are $obs
replaced by this second for the loop.
In this case, I would recommend using in_array()
instead of the second loop:
$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 />';
}
source to share