如何从时间戳的两个端点制作 15 分钟的块。比如,我有一个给定的时间跨度,比如下午 6:00 到晚上 10:00。我想将这个时间跨度分成 15 分钟的块,例如,6:00-6:156:15-6:306: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');
}
您可以使用
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>";
}
演示!