【问题标题】:php while loop terminates prematurelyphp while循环过早终止
【发布时间】:2015-10-24 18:23:33
【问题描述】:

我有不同的健身课程,中间有休息时间,需要在当天以数组形式呈现。

$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”到“14: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'
)

)

我看不出哪里错了..任何指针都非常感谢

【问题讨论】:

    标签: php arrays while-loop


    【解决方案1】:

    以下是我修复的代码。
    有关于修改的cmets,请参考。
    我希望这是你所期望的结果 ;-)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      相关资源
      最近更新 更多