2015-06-15 47 views
-1

我想創建這樣的創建可進入陣列

我們的時間開始的15分鐘,這將是我們的15點區間將是X,將在變量給出,但承擔現在的時間間隔0:30秒,所以我會得到14:30,14:00 ...直到0:00。我想將結果放入一個我可以稍後參考的數組中。

沒有想到如何實現這一點。感謝您的任何提示。

$interval = 30; //seconds 

現在我用這個陣列(INT = 15S),但我希望它給我莫名其妙更加動態的,當我想設置一個間隔。

$intervals = array(
    "15:00", 
    "14:45", 
    "14:30", 
    "14:15", 
    "14:00", 
    "13:45", 
    "13:30", 
    "13:15", 
    "13:00", 
    "12:45", 
    "12:30", 
    "12:15", 
    "12:00", 
    "11:45", 
    "11:30", 
    "11:15", 
    "11:00", 
    "10:45", 
    "10:30", 
    "10:15", 
    "10:00", 
    "09:45", 
    "09:30", 
    "09:15", 
    "09:00", 
    "08:45", 
    "08:30", 
    "08:15", 
    "08:00", 
    "07:45", 
    "07:30", 
    "07:15", 
    "07:00", 
    "06:45", 
    "06:30", 
    "06:15", 
    "06:00", 
    "05:45", 
    "05:30", 
    "05:15", 
    "05:00", 
    "04:45", 
    "04:30", 
    "04:15", 
    "04:00", 
    "03:45", 
    "03:30", 
    "03:15", 
    "03:00", 
    "02:45", 
    "02:30", 
    "02:15", 
    "02:00", 
    "01:45", 
    "01:30", 
    "01:15", 
    "01:00", 
    "00:45", 
    "00:30", 
    "00:15", 
    "00:00" 
); 
+0

可能重複http://stackoverflow.com/questions/18233794/php-時間間隔) –

回答

0
$start = strtotime('00:15:00');  // start time 
$end = strtotime('00:00');    // end time (must be less) 
$interval = 30;      // in seconds 
$arr = array(); 

do { 
    $arr[] = $i = date("i:s", $start); 
    $start = strtotime("00:" . $i . " -". $interval ." second"); 
} while($start >= $end); 

print_r($arr); 
0

你可以試一下,像這樣的:

function generateTimeArray($start, $end, $interval) { 
    $t = strtotime($end) - $interval; 
    $results = array(date("H:i:s", strtotime($end))); 
    while ($t >= strtotime($start)) { 
     $results[] = date("H:i:s", $t); 
     $t -= $interval; 
    } 
    return $results; 
} 

$results = generateTimeArray("00:00:00", "00:15:00", 15); 
print_r($results); 
0
$interval = 15; //seconds 
$time = 15; //minutes 
$sec_time = $time*(60/$interval); //We are converting 15 minutes to seconds. 

$in = array(); 
$in[0]=$time.":00"; 
$count = 0; 

while ($sec_time > 0){ 
//This will only work alright if 60 can be divided by $interval, else, you'll get floats 
    $in[count]=$sec_time%60.":".$sec_time/60; 
    $count++; 
    $sec_time = $sec_time-$interval; 
} 
print_r($in); 
[PHP的時間間隔(的