在php中计算2次之间的时间段列表?

时间:2012-07-07 06:52:53

标签: php date time period

我想在两次之间生成3个time-period-list列。

if
$start_time = '08:00 AM';
$end_time = '10:00: PM';

Time Period List As: 

Morning ----- Noon ----- Eve
8:00 AM       1:00 PM    6:00 PM
8:30 AM       1:30 PM    6:30 PM
9:00 AM       2:.0 PM    7:00 PM
to            to         to
...           ...        ...
12:00 PM      5:00 PM    10:00 PM

我计算了$ start_time和$ end_time之间的时间:

$time = time();
$rounded_time = $time % 900 > 450 ? $time += (900 - $time % 900):  $time -= $time % 900;
$start = strtotime('08:00 AM');
$end = strtotime('10:00 PM');
 for( $i = $start; $i <= $end; $i += 1800) 
 {
    echo  "<input name='start_time' type='radio' value='".date('g:i A', $i)."' />"." ".date('g:i A', $i)."<br>";
 }

如上所述,将这些时间分成三列的剩余工作

先感谢我的所有同伴。

1 个答案:

答案 0 :(得分:0)

您可以使用strtotime方法添加和减去给定时间的时间段。例如:

$time = strtotime('10:00'); 
$halfAnHourBefore = date("H:i", strtotime('-30 minutes', $time)); 
$halfAnHourAfter = date("H:i", strtotime('+30 minutes', $time)); 

将给出$ halfAnHourBefore为09:30和$ halfAnHourAfter为10:30