PHP开始时间和结束时间之间的时间循环

时间:2012-06-08 00:31:53

标签: php loops time

我有这个功能,它在select输入中提供了一组选项。 选项给我时间间隔为5分钟。 问题是当时间如23:45时,选项将从00:10开始,并基于$ j变量循环。

这就是我想用的话: 在$ open_time到$ close_time的5分钟间隔内给我一个选项列表。 如果当前时间($ timeNow)大于$ open_time,则将$ open_time设置为$ timeNow以显示为第一个选项。 这只循环直到$ close_time。

我希望这很清楚。 感谢您的帮助:)

以下是代码:

function selectTimesofDay(){
    $output = "";
    $now = date('G:i ', time()); // time now
    $timeNow = strtotime($now); // strtotime now
    $next_five = ceil($timeNow / 300) * 300; // get next 5 minute
    // time now rounded to next 10 minute
    $round5minNow = date('G:i', strtotime('+15 minutes',$next_five)); 
    $open_time = strtotime('17:00');
    $close_time = strtotime('23:59');

    // in the middle of working hours, time sets to current
    if($timeNow >= $open_time && $timeNow < $close_time){
        $open_time = strtotime($round5minNow);
     }
    $time_diff = round(($close_time - $open_time)/60) ; 
    if(date('l') == 'Friday'){
        $j = ($time_diff/5)+11; // working hours extended untill 1:00 AM
    } else{
        $j = ($time_diff/5)-1; // working hours untill 12:00 AM
    }

        for($i = 0; $i <= $j; $i++){
            $b = $i*5;  
            $data = date('l')." - ".date("H:i", strtotime('+'.$b.' minutes', $open_time));
            $output .= "<option value=\"{$data}\">";    
            $output .= $data;
            $output .= "</option>";
        }

    return $output;
}

2 个答案:

答案 0 :(得分:7)

你真正需要的是:

function selectTimesOfDay() {
    $open_time = strtotime("17:00");
    $close_time = strtotime("23:59");
    $now = time();
    $output = "";
    for( $i=$open_time; $i<$close_time; $i+=300) {
        if( $i < $now) continue;
        $output .= "<option>".date("l - H:i",$i)."</option>";
    }
    return $output;
}

所以这样做是在开始和结束之间每五分钟间隔运行一次循环检查。如果它在临时之前跳过它,否则添加一个选项。

它比你想做的更有效率,也可能更容易理解。

你甚至可以把它放在循环之后:

if( $output == "") return "<option disabled>Sorry, we're closed for today</option>";

另外,请注意我是如何一直遗漏value属性的。那是因为在没有value的情况下,选项的文本被用作值。因此,该解决方案避免了不必要的重复。

答案 1 :(得分:2)

考虑将硬编码的打开和关闭时间从功能体中取出。函数的目标是编写可以重用的代码,因此如果您的小时数发生变化,那么您不必更改函数,而是更改传递给它的参数。

// sample usage: print '<select>'.selectTimesofDay('17:00', '23:59').'</select>';
function selectTimesofDay($start=false, $end=false, $interval='5 minutes'){
    $interval = DateInterval::createFromDateString($interval);
    $rounding_interval = $interval->i * 60;
    $date = new DateTime(
        date('Y-m-d H:i', round(strtotime($start) / $rounding_interval) * $rounding_interval)
    );
    $end = new DateTime(
        date('Y-m-d H:i', round(strtotime($end) / $rounding_interval) * $rounding_interval)
    );

    $opts = array();
    while ($date < $end) {
        if ($date->getTimestamp() < time()) {
            $date->add($interval);
            continue;
        }
        $data = $date->format('l').' - '.$date->format('H:i');
        //$opts[] = '<option value="'.$date->getTimestamp().'">'.$data.'</option>'; // < -- pass the timestamp instead of a string?
        $opts[] = '<option>'.$data.'</option>';
        $date->add($interval);
    }

    return count($opts) < 1 ? 
        '<option value="-1">- closed -</option>' : 
        implode("\n", $opts);
}

<强>文档

PHP的DateTime对象 - http://www.php.net/manual/en/class.datetime.php

PHP的DateInterval对象 - http://www.php.net/manual/en/dateinterval.format.php

PHP函数 - http://www.php.net/manual/en/functions.user-defined.php

PHP函数教程 - http://www.tizag.com/phpT/phpfunctions.php

相关问题