While循环中的嵌套foreach循环可以使while循环的条件结束吗?

时间:2015-06-22 16:16:35

标签: php loops foreach while-loop

以下是我想要创建的内容的一些背景知识。

我正在创建一个名为getNextBilling($dateStart,$dateCount = 20)

的函数

你给它一个句号长度,这是你想要某人收费的日子

$test->period = '2,5,15';

需要一个开始日期 我已经在测试页面上分配了

$test->getNextBilling('2015-06-12 00:00:00',2);

该功能应该做的是确保输入日期是否小于当前日期以跳过此月份结算,这非常有效。

我的while循环中有一个foreach循环来查看所有的日期,我无法找到一种方法让代码在实际的地方工作只显示您想要的天数。

上面的示例显示我想显示 2 天输出

Array
(
    [1] => 2015-06-15
    [2] => 2015-07-02
    [3] => 2015-07-05
    [4] => 2015-07-15
)

这是函数的代码

// SET START DATE
$startDate = new DateTime($dateStart);
// LOOP
$i = 1;
while($i <= $dateCount){
    // LOOP THROUGH INPUTED DAYS
    foreach($days as $day){
        // CLEAN DAY
        $day = formatNumbersOnly($day);                     
        // SET LAST DAY
        $lastDay = new DateTime($startDate->format('Y-m-d'));
        $lastDay->modify('last day of this month');
        // CHECK FOR PASSED DAY
        if($day > $startDate->format('j')){
            // CHECK FOR 28-29-30-31
            if($day > $lastDay->format('j')){
                // SET NEW DATE
                $startDate->setDate(
                    $startDate->format('Y'),
                    $startDate->format('m'),
                    $lastDay->format('j')
                );
            } else {
                // SET NEW DATE
                $startDate->setDate(
                    $startDate->format('Y'),
                    $startDate->format('m'),
                    $day
                );
            }
            // SET ARRAY
            $nextBilling[$i] = $startDate->format('Y-m-d');

        } else {
            // SKIP
            $i--;
        }
        // INCREASE COUNT
        $i++;
    }               
    // NEXT MONTH
    $startDate->modify('1 month');
    $startDate->setDate($startDate->format('Y'),$startDate->format('m'),1);
}

现在我明白了问题出在哪里,因为foreach循环内部的计数增加了,但是当我把它放在外面时它只会在这样的日子里输出最后一天:< / p>

Array ( [-1] => 2015-06-15 [0] => 2015-07-15 [1] => 2015-08-15 [2] => 2015-09-15 )

如果有人有任何批评或提示,将不胜感激。我将继续努力解决它。

编辑:我在foreach的开头添加了if语句,以检查$i是否大于$dateCount它是否会突破循环。感谢Twisty的时间和精力,我很感激。

1 个答案:

答案 0 :(得分:0)

很高兴你找到了答案。这是我在找到它时正在做的事情。

// SET START DATE
$startDate = new DateTime($dateStart);
$nextBill = new DateTime();

for($i=1;$i<$countDays;$i++){
    switch(true){
        case ($startDate->format('j') > $days[2]):
            // go to next month
            $nextBill->setDate(
                $startDate->format('Y'),
                $startDate->format('m')+1,
                $days[0];
            );
            break;
        case ($startDate->format('j') > $days[1]):
            // Is start date greater than 2nd billing (5)
            $nextBill->setDate(
                $startDate->format('Y'),
                $startDate->format('m'),
                $days[2];
            );
            break;
        case ($startDate->format('j') > $days[0]):
            // is starteDate greater than 1st billing (2)
            $nextBill->setDate(
                $startDate->format('Y'),
                $startDate->format('m'),
                $days[1];
            );
            break;
        default:
            $nextBill->setDate(
                $startDate->format('Y'),
                $startDate->format('m'),
                $days[0];
            );
    }
    $nextBilling[$i] = $nextBill->format('Y-m-d');
    $startDate = $nextBill->setDate(
        $nextBill->format('Y'),
        $nextBill->format('m'),
        $nextBill->format('j')+1
    );
}

这将确保如果$startDate是本月的第1天,它会显示第2个,然后是第5个。 Id $startDate是第3,它将显示第5和第15。如果$startDate是第20个,它将显示下个月的第2个和第5个。

相关问题