Php 我如何从时间戳的两个端点生成15分钟的块

Php 我如何从时间戳的两个端点生成15分钟的块,php,for-loop,Php,For Loop,我如何从时间戳的两个端点处划分出15分钟。比如,我有一个给定的时间跨度,比如说下午6点到晚上10点。我想把这段时间分成15分钟, 6:00-6:15 6:15-6:30 6:30-6:45等至晚上10:00 任何人都可以帮忙吗?听起来你需要这样的东西: $tz = new DateTimeZone('UTC'); $from = new DateTime('2013-11-13 18:00:00', $tz); $to = new DateTime('2013-11-13 22:0

我如何从时间戳的两个端点处划分出15分钟。比如,我有一个给定的时间跨度,比如说下午6点到晚上10点。我想把这段时间分成15分钟, 6:00-6:15 6:15-6:30 6:30-6:45等至晚上10:00


任何人都可以帮忙吗?

听起来你需要这样的东西:

$tz    = new DateTimeZone('UTC');
$from  = new DateTime('2013-11-13 18:00:00', $tz);
$to    = new DateTime('2013-11-13 22:00:00', $tz);
$times = array();

while ($from <= $to) {
    $times[] = $from->format('r');
    $from->modify('+15 minutes');
}
$tz=新的日期时区('UTC');
$from=新日期时间($2013-11-13 18:00:00’,$tz);
$to=新日期时间($2013-11-13 22:00:00’,$tz);
$times=array();
而($from格式('r');
$from->modify(“+15分钟”);
}

您可以使用
DatePeriod
类:

$begin = new DateTime('6:00 PM');
$end = new DateTime('10:00 PM');
$end = $end->modify('+15 minutes'); // to get the last interval, too

$interval = new DateInterval('PT15M');
$timerange = new DatePeriod($begin, $interval ,$end);

foreach($timerange as $time){
    echo $time->format("h:i") . "<br>";
}
$begin=newdatetime('6:00pm');
$end=新的日期时间(“晚上10:00”);
$end=$end->modify(“+15分钟”);//获取最后一次间隔
$interval=新的日期间隔('PT15M');
$timerange=newdateperiod($begin,$interval,$end);
foreach($time作为$time的时间范围){
echo$time->format(“h:i”)。“
”; }

你想如何使用这些时间戳?你只是想要一个时间的输出吗?你想要一个时间数组吗?DateTime对象?看起来你已经这样做了。你在将示例转换为代码时遇到了什么问题?喜欢这个。
DatePeriod
对我来说是一个新的问题!