从接下来 10 天的数组中删除周末

Remove weekend from array of the next 10 days

我创建了一个未来 10 天的数组,有 2 天的缓冲区(即如果是星期一,该数组从星期三开始)。我现在正试图从数组中删除周末,但不确定如何去做。下面是我的 PHP 和返回的数组:

    $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);

这个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 
)

谁能帮我理解如何从上面过滤掉任何星期六或星期日

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);

这对 DatePeriod class 来说是件好事。我们设置了从开始时间(2 天内)开始 2 天的 10 次重复周期,然后可以遍历日期,检查周末(星期几 = 0 或 6)以将其从输出中排除:

$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);

输出:

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
)

如果您想要从今天起的 2 天起连续 10 天(不包括周末),您只需将代码的第二行更改为:

$period = new DatePeriod($start, new DateInterval('P1D'), 9);

输出为:

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
)

Demo on 3v4l.org

这是一个使用 while 循环的简单答案。

https://3v4l.org/0lpGX

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

?>

打印出来

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
)