数组$weekDays
包含从今天开始的7天。
2017-06-01/ 2017-06-02 / 2017-06-03 / 2017-06-04 / 2017-06-05 / 2017-06-06 / 2017-06-07
$weekDays[0] = date('Y-m-d');
for ($i=1; $i<7; $i++){
$weekDays[$i] = date('Y-m-d', strtotime('+' . $i . ' days'));
}
我只需要工作日,所以我试过这个:
for ($j=0; $j < 7; $j++) {
$weekDays[$j] = strtotime($weekDays[$j]);
$weekday = date('w', $weekDays[$j]);
if ($weekday == 0 || $weekday == 6) {
echo "Saturday or Sunday<br>";
} else {
echo $weekday . "<br>";
}
}
但是我想只用工作日和Y-m-d
格式返回数组。
提前致谢!
答案 0 :(得分:3)
strtotime()
很聪明,你可以要求它只返回工作日。
for($d=0; $d<5; ++$d){
$result[]=date('Y-m-d',strtotime("+$d weekdays"));
}
var_export($result);
无需后置环路过滤。
输出:
array (
0 => '2017-06-01',
1 => '2017-06-02',
2 => '2017-06-05',
3 => '2017-06-06',
4 => '2017-06-07',
)
答案 1 :(得分:0)
您离解决方案更近了一步,您可以使用unset从阵列中删除非工作日:
for ($j=0; $j < 7; $j++)
{
$weekDays[$j] = strtotime($weekDays[$j]);
$weekday = date('w', $weekDays[$j]);
if ($weekday == 0 || $weekday == 6)
{
unset($weekDays[$j]);
}
}
$weekDays = array_values($weekDays); //reorder after all unsets