输出是 Day 1 2015-03-10---there Day 2 2-6ren">
gpt4 book ai didi

php - 如何区分日期列表之间的日期? PHP

转载 作者:可可西里 更新时间:2023-11-01 01:00:04 25 4
gpt4 key购买 nike

我一直在努力列出我上学和不上学的日子。

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

<?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

我不明白为什么它在第 2 天不说“不存在”2015-03-11第 5 天 2015-03-14,请有人帮助我,我已经用了几个小时了。

最佳答案

一旦找到针,您应该添加一个break:

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";
}

关于php - 如何区分日期列表之间的日期? PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29961215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com