两个日期之间的所有日期,间隔为30分钟

时间:2011-12-22 14:31:49

标签: php datetime

我有$ 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?

3 个答案:

答案 0 :(得分:5)

这样的事情应该有效:

$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);

答案 1 :(得分:3)

你可以这样做:

<?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 />";
?>

答案 2 :(得分:2)

看看你如何使用它..

$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.
}