2012-06-08 71 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

謝謝克里斯。這也是一個很好的解決方案。乾杯。 –

相關問題