两个约会之间的所有约会,间隔30分钟


All dates between two dates with 30 minutes intervals

我有$one = '2011-12-22 07:00 '和$two = '2011-12-22 11:30';

我想以30分钟的间隔接收所有数据。对于这个例子应该是:

2011-12-22 07:00
2011-12-22 07:30
2011-12-22 08:00
2011-12-22 08:30
2011-12-22 09:00
2011-12-22 09:30
2011-12-22 10:00
2011-12-22 10:30
2011-12-22 11:00
2011-12-22 11:30

是否可以使用循环FOR?

应该这样做:

$dates = array();
$start = new DateTime($one);
$until = new DateTime($two);
while($start->getTimestamp() <= $until->getTimestamp()) {
   $dates[] = clone $start;
   $start->add(new DateInterval("PT30M"));
}
echo '<pre>';
print_r($dates);

你可以这样做:

<?php
$interval = 1800; // Interval in seconds
$date_first     = "2011-12-22 07:00";
$date_second    = "2011-12-22 11:30";
$time_first     = strtotime($date_first);
$time_second    = strtotime($date_second);
for ($i = $time_first; $i < $time_second; $i += $interval)
    echo date('Y-m-d H:i', $i) . "<br />";
?>

看看你是怎么做的。

$one = strtotime('2011-12-22 07:00');
$two = strtotime('2011-12-22 11:30');
while($two>$one){
    $interval_timestamp = $one += 60*30;
    echo date('r',$interval_timestamp);//output as needed.
}