2012-03-28 40 views
0

我想填充一個選擇列表,使它從00:00開始直到23:55每增加5分鐘。我也需要在UNIX時間做到這一點。如何隨時間填充選擇列表?

下面是我所做的,但我剛剛意識到做strtotime("18:00")實際上並沒有給我具體的時間。我也有一個選擇的日期列表,例如日期可以是「2012年3月6日星期二」(其中unix格式爲:1330992000)。我希望能夠獲取兩個選定的值,然後將兩個unix時間戳添加在一起,以便將其插入到數據庫中。例如,用戶選擇時間17:00,這將給我1332950400(我知道這是錯誤的),然後是2012年3月6日星期二,這將給我1330992000。然後,我希望能夠將這兩個時間戳添加到一起,以便在2012年3月6日星期二18:00以Unix時間戳格式獲得。

public function timeSelectList() 
{ 
    //using actual unix time instead of strtotime to save it from having to call the function each time. 
    $startTime = 1332889200; //strtotime('00:00'); 
    $endTime = 1332975300; //strtotime('23:55'); 
    $now = $startTime; 

    $startSelectList = '<label for="startSelect">Start Time</label><select name="startSelect" id="startSelect">'; 
    $endSelectList = '<label for="endSelect">End Time</label><select name="endSelect" id="endSelect">'; 
    //populates the select list with the starting date of the course up to the next six months 
    while($now <= $endTime) 
    { 
     if($now == 1332950400)// strtotime('17:00') 
     { 
      $startSelectList .= '<option value="'.$now.'" selected="selected">'.date('H:i', $now).'</option>'; 
      $endSelectList .= '<option value="'.$now.'">'.date('H:i', $now).'</option>'; 
     } 
     else if($now == 1332954000)//strtotime('18:00') 
     { 
      $startSelectList .= '<option value="'.$now.'">'.date('H:i', $now).'</option>'; 
      $endSelectList .= '<option value="'.$now.'" selected="selected">'.date('H:i', $now).'</option>'; 
     } 
     else 
     { 
      $startSelectList .= '<option value="'.$now.'">'.date('H:i', $now).'</option>'; 
      $endSelectList .= '<option value="'.$now.'">'.date('H:i', $now).'</option>'; 
     } 
     $now += 300; //increment 5 minutes (300 seconds = 5 minutes 
    } 

    $startSelectList .= '</select>'; 
    $endSelectList .= '</select>'; 

    return $startSelectList.$endSelectList; 
} 

回答

0

這是一個有點不清楚的結果形式是什麼樣子。儘管如此,在這些情況下,更容易做字符串數學日期數學

例如,將日期和時間一起添加爲一個字符串,然後確定它是時間戳。

$date_string = $date . ' ' . $time; // '2012-03-28 18:00' 
$timestamp = strtotime($date_string); 

注:你當然要事先做一些驗證或字符串格式化。但上面應該是你需要的最後一塊。