2012-06-08 73 views
1

我有这个功能,它给了我在选择输入中的一组选项。 选项给我5分钟的时间间隔。 问题是当时间是23:45时,选项将从00:10开始,并基于$ j变量进行循环。PHP开始时间和结束时间之间的时间循环

这就是我想要用文字所做的: 给我一个从$ 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; 
} 
+0

那么上面的代码有什么问题? –

+0

当客户端计算机上的时间达到23:45时,选项集显示从00:15开始并持续到23:55的时间! –

回答

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的情况下,该选项的文本被用作值。因此这个解决方案避免了不必要的重复。

+0

+1 [[time()'](http://br.php.net/manual/en/function.time.php)。 –

+0

谢谢亲爱的Kolink。这太棒了。欣赏它。 –

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

+0

谢谢克里斯。这也是一个很好的解决方案。干杯。 –

相关问题