如何在特定的持续时间内划分2次

时间:2017-06-19 05:48:10

标签: php

有2次$dayFrom = 10:00:00;$dayTo = 12:00:00我希望在15分钟的持续时间内将其除以。

预期结果

10:00:00, 10:15:00, 10:30:00, 10:45:00 and so on

寻求帮助

4 个答案:

答案 0 :(得分:1)

$timeArray = array();
$startTime  = new \DateTime("2010-01-01 10:00:00");
$endTime    = new \DateTime("2010-01-01 12:00:00");
while($startTime < $endTime) {
    $timeArray[] = $startTime->format('H:i:s');
    $startTime->add(new \DateInterval('PT 15 M'));
}
echo implode(",",$timeArray);

您可以使用此代码。它会对你有帮助:))

答案 1 :(得分:1)

你也可以试试这个 -

$dayFrom = strtotime('10:00:00');
$dayTo = strtotime('12:00:00');

while($dayFrom <= $dayTo) {
   echo date('H:i:s', $dayFrom);
   $dayFrom= strtotime('+ 15 MINUTES', $dayFrom);
}

<强>输出

10:00:00
10:15:00
10:30:00
10:45:00
11:00:00
11:15:00
11:30:00
11:45:00
12:00:00

strtotime()

答案 2 :(得分:1)

主题的另一个变体 - 使用DateInterval。

$df='H:i';
$timezone=new DateTimeZone('Europe/London');
$interval=new DateInterval('PT15M');

$ts=date( 'Y-m-d H:i:s', strtotime('10.00am') );
$tf=date( 'Y-m-d H:i:s', strtotime('12.00pm') );

$start=new DateTime( $ts, $timezone );
$end=new DateTime( $tf, $timezone );

while( $start->add( $interval ) <= $end ){
    echo $start->format( $df ).'<br />';
}

答案 3 :(得分:0)

$dayFrom = "10:00:00";
$dayTo = "12:00:00";
while($endTime<$dayTo){   
$endTime = strtotime($dayFrom) + 900;
echo date('h:i:s', $endTime);
}

试试这个。

相关问题