我有一个转换为字符串的数组,它看起来像这样:00:00,05:00,10:00,15:00,20:00
它显示每5小时的小时范围,
我想显示我的字符串, , , ,5:00, , , , ,10:00, , , , ,15:00, , , , ,20:00, , , ,
所以每5小时显示一次,但不需要的小时我想设置为空,
代码:
$date = new \DateTime("12:00am");
$dv = new \DateInterval('PT5H');
$totalInterval = 4 ;
$dr = array();
$date->format('H:i');
for($i = 0; $i < $totalInterval; $i++)
{
$dr[] = $date->format('H:i');
$date->add($dv);
}
$dr[] = $date->format('H:i');
$dr = implode (',', $dr);
我该怎么做?
答案 0 :(得分:2)
将$dr[] = $date->format('H:i');
更改为:
array_push($dr, '', '', '', '', $date->format('H:i'))
答案 1 :(得分:2)
$dr = ' , , , ,' . implode (', , , , ,', $dr) . ', , , , ';
答案 2 :(得分:1)
您可以添加必要的空条目:
$hours = 5;
$dv = new \DateInterval('PT'.$hours.'H');
...
for( $i=0; $i<$totalInterval; $i++) {
for( $off=0; $off<$hours-1; $off++) $dr[] = " ";
$dr[] = $date->format('H:i');
...
}
...
echo implode(',',$dr);