gpt4 book ai didi

php - 在循环内添加或插入条件

转载 作者:行者123 更新时间:2023-11-29 17:29:06 25 4
gpt4 key购买 nike

我有一个在两个日期之间循环的 php 脚本。

$begin = new DateTime("2018-06-01");  
$end = new DateTime("2018-06-30");

for($i = $begin; $i <= $end; $i->modify('+1 day')){
echo $i->format("Y-m-d");
}

然后我有一个 mysql 数据结果(5 行),其中包含星期五的日期。
2018-06-01 - 星期五
2018-06-08 - 星期五
2018-06-15 - 星期五
2018-06-22 - 星期五
2018-06-29 - 星期五

我如何将其集成到 for 循环中,以便如果星期五日期匹配我可以回显某些内容。

我必须在for循环内执行foreach循环,但问题是它重复,这实际上是我的mysql数据结果中返回的包含星期五日期的行数。

for($i = $begin; $i <= $end; $i->modify('+1 day')){  
foreach($result as $row){
if($i->format("Y-m-d")!=$row['rest_day']){ echo $i->format("Y-m-d"); }
else{ echo 'Rest Day';}
}
}

The display here is that for each day it is duplicated 5 times, i think it is because of the number of fridays which is 5, this is actually the number of rows returned from the mysql data result.

FRIDAYS只是一个例子,它可能会不时发生变化,所以基本上它是动态的。这里最好的方法是什么?

最佳答案

您可以使用 date('D') 从常规日期格式检查日期代码。

$begin = new DateTime("2018-06-01");  
$end = new DateTime("2018-06-30");

for($i = $begin; $i <= $end; $i->modify('+1 day')){
if($i->date('D') == 'Fri'){
//echo something
}

}

关于php - 在循环内添加或插入条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50785965/

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