我在php中有开始时间和结束时间。
如果我给出持续时间,它应该显示所有时间间隔
Startime = 2014-07-28 07:00:00
End Time = 2014-07-28 11:00:00
duration = 30 min
我需要在开始和结束时间之间有30分钟的差异输出。
输出应如下所示:
07:00,07:30,08:00,08:30 ..... 10:00,10:30,11:00
答案 0 :(得分:1)
试
$s = strtotime("2014-07-28 07:00:00");
$e = strtotime("2014-07-28 11:00:00");
while($s != $e) {
$s = strtotime('+30 minutes', $s);
echo date('H:i', $s);
}
输出: - 07:30 08:00 08:30 09:00 09:30 10:00 10:30 11:00
逗号分隔的: -
while($s != $e) {
$s = strtotime('+30 minutes', $s);
$arr[] = date('H:i', $s);
}
echo implode(',', $arr);
输出: - 07:30,08:00,08:30,09:00,09:30,10:00,10:30,11:00
答案 1 :(得分:0)
使用DatePeriod
类:
$start = new DateTime('2014-07-28 07:00:00');
$end = new DateTime('2014-07-28 11:00:00');
$interval = new DateInterval('PT30M');
$period = new DatePeriod($start, $interval, $end);
foreach($period as $time) {
echo $time->format('Y-m-d H:i:s') . PHP_EOL;
}
输出:
2014-07-28 07:00:00
2014-07-28 07:30:00
2014-07-28 08:00:00
2014-07-28 08:30:00
2014-07-28 09:00:00
2014-07-28 09:30:00
2014-07-28 10:00:00
2014-07-28 10:30:00
答案 2 :(得分:0)
你可以尝试
$start = "2014-07-28 07:00:00";
$end = "2014-07-28 11:00:00";
$start_time = strtotime($start);
$end_time = strtotime($end);
$time_diff = 30 * 60;
for($i=$start_time; $i<=$end_time; $i+=$time_diff)
{
echo date("H:i", $i).", ";
}
请参阅WORKING DEMO