我想在兩次之間創建一個時間範圍。我可以在PHP 這個代碼給我的時間間隔30分鐘,當我提供開始時間,結束時間和時間間隔。下面是php腳本。如何在Java中的開始時間和時間之間找出時間範圍數組?
//timerange.php
<?php
/**
* create_time_range
*
* @param mixed $start start time, e.g., 9:30am or 9:30
* @param mixed $end end time, e.g., 5:30pm or 17:30
* @param string $by 1 hour, 1 mins, 1 secs, etc.
* @access public
* @return void
*/
function create_time_range($start, $end, $by='30 mins') {
$start_time = strtotime($start);
$end_time = strtotime($end);
$current = time();
$add_time = strtotime('+'.$by, $current);
$diff = $add_time-$current;
$times = array();
while ($start_time < $end_time) {
$times[] = $start_time;
$start_time += $diff;
}
$times[] = $start_time;
return $times;
}
// create array of time ranges
$times = create_time_range('9:30', '17:30', '30 mins');
// more examples
// $times = create_time_range('9:30am', '5:30pm', '30 mins');
// $times = create_time_range('9:30am', '5:30pm', '1 mins');
// $times = create_time_range('9:30am', '5:30pm', '30 secs');
// and so on
// format the unix timestamps
foreach ($times as $key => $time) {
$times[$key] = date('g:i:s', $time);
}
print '<pre>'. print_r($times, true).'</pre>';
/*
* result
*
Array
(
[0] => 9:30:00
[1] => 10:00:00
[2] => 10:30:00
[3] => 11:00:00
[4] => 11:30:00
[5] => 12:00:00
[6] => 12:30:00
[7] => 1:00:00
[8] => 1:30:00
[9] => 2:00:00
[10] => 2:30:00
[11] => 3:00:00
[12] => 3:30:00
[13] => 4:00:00
[14] => 4:30:00
[15] => 5:00:00
[16] => 5:30:00
)
*/
?>
我需要在JAVA代碼中做同樣的事情。我認爲這對其他人會有幫助。
Java不是縮寫,不應該設置爲全部大寫。 – 2011-12-24 11:50:22
謝謝..現在我編輯了。 – mH16 2011-12-24 11:52:52