Reputation: 119
The array $weekDays
contains 7 days that starts today.
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'));
}
I only need the weekdays so I tried this:
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>";
}
}
But I want to return the array with only the weekdays and in Y-m-d
format.
Thanks in advance!
Upvotes: 2
Views: 117
Reputation: 47903
strtotime()
is pretty smart, you can ask it to only return weekdays.
for($d=0; $d<5; ++$d){
$result[]=date('Y-m-d',strtotime("+$d weekdays"));
}
var_export($result);
No post loop filtering required.
Output:
array (
0 => '2017-06-01',
1 => '2017-06-02',
2 => '2017-06-05',
3 => '2017-06-06',
4 => '2017-06-07',
)
Upvotes: 3
Reputation: 1535
You're one step closer to get your solution, you can use unset to remove non-weekdays from your array:
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
Upvotes: 0