php while while循环过早终止

时间:2015-10-20 18:41:17

标签: php arrays while-loop

我有不同的健身课程,其间需要在当天的阵列中展示。

$sStart = '08:00';
$startMin = 480   //start time in minutes
$br1MS = 560  //first break in minutes
$br2MS = 780   //second break in minutes
$sEnd = '16:00';
$interval = 40;

现在我的第一次休息时间是在两次会议之后,即09:20'到' 09:40'并且第二次和最后一次会议开始了#13; 00'至' 00'。

我在firstBreak之前使用了一段时间来填充数组:

while ($startMin < $br1MS) {
        if(isset($t['end']) && ($t['end'] == date('H:i',(strtotime(floor($startMin/60).":".$startMin%60))))){
            $t['start'] = $t['end'];
            $nStart = $startMin+$interval;
            $t['end'] = date('H:i',(strtotime(floor($nStart/60).':'.$nStart%60)));
            $timeArray[] = $t;
            $s['bStart'] = $t['end']; //start time
            $s['bEnd'] = date('H:i',(strtotime(floor($br1ME/60).':'.$br1ME%60)));
            $timeArray[] = $s;
            $startMin = $br1ME;
        }else{
            $t['start'] = date('H:i',(strtotime(floor($startMin/60).':'.$startMin%60)));
            $startMin += $interval;
            $t['end'] = date('H:i',(strtotime(floor($startMin/60).':'.$startMin%60)));
            $timeArray[] = $t;
        }
    }

结果是:

array(
(int) 0 => array(
    'start' => '08:00',
    'end' => '08:40'
),
(int) 1 => array(
    'start' => '08:40',
    'end' => '09:20'
),
(int) 2 => array(
    'bStart' => '09:20',
    'bEnd' => '09:40'
))

然后我用会话填充数组,直到第二次打破另一个while循环,只是复制上面的第一个,如下所示:

while ($startMin < $br2MS) {
        debug($br2MS);
        if(isset($t['end']) && ($t['end'] == date('H:i',(strtotime(floor($startMin/60).":".$startMin%60))))){
            $t['start'] = $t['end'];
            $nStart = $startMin+$interval;
            $t['end'] = date('H:i',(strtotime(floor($nStart/60).':'.$nStart%60)));
            $timeArray[] = $t;
            $s['bStart'] = $t['end']; //start time
            $s['bEnd'] = date('H:i',(strtotime(floor($br2ME/60).':'.$br2ME%60)));
            $timeArray[] = $s;
            $startMin = $br2ME;
        }else{
            $t['start'] = date('H:i',(strtotime(floor($startMin/60).':'.$startMin%60)));
            $startMin += $interval;
            $t['end'] = date('H:i',(strtotime(floor($startMin/60).':'.$startMin%60)));
            $timeArray[] = $t;
        }
    }

结果是:

array(
(int) 0 => array(
    'start' => '08:00',
    'end' => '08:40'
),
(int) 1 => array(
    'start' => '08:40',
    'end' => '09:20'
),
(int) 2 => array(
    'bStart' => '09:20',
    'bEnd' => '09:40'
),
(int) 3 => array(
    'start' => '09:40',
    'end' => '10:20'
),
(int) 4 => array(
    'start' => '10:20',
    'end' => '11:00'
),
(int) 5 => array(
    'bStart' => '11:00',
    'bEnd' => '14:00'
)

问题:

第二个循环仅在两次迭代后终止,而不填充所有第二批会话。 这是我的预期:

array(
(int) 0 => array(
    'start' => '08:00',
    'end' => '08:40'
),
(int) 1 => array(
    'start' => '08:40',
    'end' => '09:20'
),
(int) 2 => array(
    'bStart' => '09:20',
    'bEnd' => '09:40'
),
(int) 3 => array(
    'start' => '09:40',
    'end' => '10:20'
),
(int) 4 => array(
    'start' => '10:20',
    'end' => '11:00'
),
(int) 5 => array(
    'start' => '11:00',
    'end' => '11:40'
),
(int) 6 => array(
    'start' => '11:40',
    'end' => '12:20'
),
(int) 7 => array(
    'start' => '12:20',
    'end' => '13:00'
),
(int) 8 => array(
    'bStart' => '13:00',
    'bEnd' => '14:00'
)

我无法看到哪里出错了。非常感谢指点

1 个答案:

答案 0 :(得分:0)

以下是我修复过的代码 有关于修改的意见,请参阅 我希望这是你期望的结果; - )

<?php
date_default_timezone_set('Asia/Tokyo');
$sStart = '08:00';
$startMin = 480;   //start time in minutes
$br1MS = 560;  //first break in minutes
$br1ME = 580;  /** Added */
$br2MS = 780;   //second break in minutes
$br2ME = 840;   /** Added */
$sEnd = '16:00';
$interval = 40;

/** Not modified */
while ($startMin < $br1MS) {
    if(isset($t['end']) && ($t['end'] == date('H:i',(strtotime(floor($startMin/60).":".$startMin%60))))){
        $t['start'] = $t['end'];
        $nStart = $startMin+$interval;
        $t['end'] = date('H:i',(strtotime(floor($nStart/60).':'.$nStart%60)));
        $timeArray[] = $t;
        $s['bStart'] = $t['end']; //start time
        $s['bEnd'] = date('H:i',(strtotime(floor($br1MS/60).':'.$br1ME%60)));
        $timeArray[] = $s;
        $startMin = $br1ME;
    }else{
        $t['start'] = date('H:i',(strtotime(floor($startMin/60).':'.$startMin%60)));
        $startMin += $interval;
        $t['end'] = date('H:i',(strtotime(floor($startMin/60).':'.$startMin%60)));
        $timeArray[] = $t;
    }
}


while ($startMin < $br2ME) {/** $br2MS -> $br2ME */
    // debug($br2MS);
    /**
     * process for second break.
     */
    if ($startMin === $br2MS) {
        $t['start'] = $t['end'];
        $nStart = $startMin + ($br2ME - $br2MS);
        $s['bStart'] = $t['end']; //start time
        $s['bEnd'] = date('H:i',(strtotime(floor($br2ME/60).':'.$br2ME%60)));

        $timeArray[] = $s;
        $startMin = $br2ME;
        continue;
    }

    if(isset($t['end']) && ($t['end'] == date('H:i',(strtotime(floor($startMin/60).":".$startMin%60))))){
        $t['start'] = $t['end'];

        $nStart = $startMin+$interval;
        $t['end'] = date('H:i',(strtotime(floor($nStart/60).':'.$nStart%60)));
        $timeArray[] = $t;

        /**
         * For the sake of clarity, the codes for second break are moved.
         */
        // $s['bStart'] = $t['end']; //start time
        // $s['bEnd'] = date('H:i',(strtotime(floor($br2MS/60).':'.$br2ME%60)));
        // $timeArray[] = $s;
        // $startMin = $br2MS;

        $startMin = $nStart;
    }else{
        $t['start'] = date('H:i',(strtotime(floor($startMin/60).':'.$startMin%60)));
        $startMin += $interval;
        $t['end'] = date('H:i',(strtotime(floor($startMin/60).':'.$startMin%60)));
        $timeArray[] = $t;
    }
}

var_dump($timeArray);