Reputation: 3697
I have created an array of the next 10 days, with a 2 days buffer (i.e. if it is a Monday, the array starts on Wednesday). I am now trying to remove weekends from my array but unsure how to go about doing this. Below is my PHP and the returned array:
$date_buffer = strtotime('+2 days');
$days = array();
for ($i = 0; $i < 10; $i++) {
$days[date($date_buffer)] = date("l, jS M", $date_buffer);
$date_buffer = strtotime('+2 days', $date_buffer);
}
print_r($days);
This returns:
Array (
[1548192409] => Tuesday, 22nd Jan
[1548365209] => Thursday, 24th Jan
[1548538009] => Saturday, 26th Jan
[1548710809] => Monday, 28th Jan
[1548883609] => Wednesday, 30th Jan
[1549056409] => Friday, 1st Feb
[1549229209] => Sunday, 3rd Feb
[1549402009] => Tuesday, 5th Feb
[1549574809] => Thursday, 7th Feb
[1549747609] => Saturday, 9th Feb
)
Can somebody help me understand how I would filter out any Saturdays or Sundays from the above
Upvotes: 0
Views: 343
Reputation: 147226
This is a good job for the DatePeriod
class. We set up a period of 10 recurrences of 2 days from the start time (in 2 days), and then can iterate through the dates, checking for a weekend day (day of week = 0 or 6) to exclude them from the output:
$start = new DateTime('+2 days');
$period = new DatePeriod($start, new DateInterval('P2D'), 9);
foreach ($period as $date) {
$dow = (int)$date->format('w');
if ($dow != 0 && $dow != 6) {
$days[$date->format('U')] = $date->format('l, jS M');
}
}
print_r($days);
Output:
Array (
[1548194036] => Tuesday, 22nd Jan
[1548366836] => Thursday, 24th Jan
[1548712436] => Monday, 28th Jan
[1548885236] => Wednesday, 30th Jan
[1549058036] => Friday, 1st Feb
[1549403636] => Tuesday, 5th Feb
[1549576436] => Thursday, 7th Feb
)
If you wanted 10 consecutive days (excluding weekends) from 2 days from today, you would just change the second line of the code to:
$period = new DatePeriod($start, new DateInterval('P1D'), 9);
and the output would be:
Array (
[1548197829] => Tuesday, 22nd Jan
[1548284229] => Wednesday, 23rd Jan
[1548370629] => Thursday, 24th Jan
[1548457029] => Friday, 25th Jan
[1548716229] => Monday, 28th Jan
[1548802629] => Tuesday, 29th Jan
[1548889029] => Wednesday, 30th Jan
[1548975429] => Thursday, 31st Jan
)
Upvotes: 1
Reputation: 4783
Here is a simple answer using the while loop.
<?php
$x = 1; // Start
$y = 10; // Iterations Needed
$days = []; //Empty Array
while($x <= $y) {
// Set Buffer
$buffer = 2 + $x;
// Get Date with Buffer
$date = date(strtotime("+$buffer days"));
// If the day is a weeday
if(date('N', $date) < 6){
// Add to array
$days[$date] = date("l, jS M", $date);
// If not, increase max iteration (example: 10 to 11)
}else{
$y++;
}
// Go to next loop
$x++;
}
echo "<pre>";
print_r($days);
?>
Which prints out
Array
(
[1548283397] => Wednesday, 23rd Jan
[1548369797] => Thursday, 24th Jan
[1548456197] => Friday, 25th Jan
[1548715397] => Monday, 28th Jan
[1548801797] => Tuesday, 29th Jan
[1548888197] => Wednesday, 30th Jan
[1548974597] => Thursday, 31st Jan
[1549060997] => Friday, 1st Feb
[1549320197] => Monday, 4th Feb
[1549406597] => Tuesday, 5th Feb
)
Upvotes: 0
Reputation: 304
http://php.net/manual/en/function.date.php
$date_buffer = strtotime('+2 days');
$days = array();
for ($i = 0; $i < 10; $i++) {
if (!in_array(date('w',$date_buffer), [0,6])) {
$days[date($date_buffer)] = date("l, jS M", $date_buffer);
}
$date_buffer = strtotime('+2 days', $date_buffer);
}
print_r($days);
Upvotes: 2