我有一个php循环如下:
<?php foreach ($dcr_all as $key=>$d):?>
<tr>
<td><?php echo $key+1?></td>
<td >
<?php $date_in = $d['DailyCallReport']['datetime_in'];
$date_in = new DateTime($date_in);
echo $date_in = $date_in -> format('h:i a');
?>
</td>
</tr>
<?php endforeach;?>
我的问题是,如果两行之间的时差($date_in)
大于1小时,如何在当前行和上一行之间插入空白行。
这是数组:
Array
(
[0] => Array
(
[DailyCallReport] => Array
(
[datetime_in] => 2016-02-25 02:00:00
)
)
[1] => Array
(
[DailyCallReport] => Array
(
[datetime_in] => 2016-02-25 02:11:00
)
)
[2] => Array
(
[DailyCallReport] => Array
(
[datetime_in] => 2016-02-25 02:23:00
)
)
[3] => Array
(
[DailyCallReport] => Array
(
[datetime_in] => 2016-02-25 04:00:00
)
)
[4] => Array
(
[DailyCallReport] => Array
(
[datetime_in] => 2016-02-25 04:12:00
)
)
[5] => Array
(
[DailyCallReport] => Array
(
[datetime_in] => 2016-02-25 06:00:00
)
)
[6] => Array
(
[DailyCallReport] => Array
(
[datetime_in] => 2016-02-25 06:09:00
)
)
)
这是我的表的图像..在第3行和第4行中,它们之间的时间差超过1小时,所以我只想显示一个空白行,其中包含没有索引号的消息.. < / p>
答案 0 :(得分:0)
试试这样:
<?php
$dcr_all = Array
(
0 => array (
'DailyCallReport' => Array
(
'datetime_in' => '2016-02-25 02:00:00'
)
),
1 => Array
(
'DailyCallReport' => Array
(
'datetime_in' => '2016-02-25 02:11:00'
)
),
2 => Array
(
'DailyCallReport' => Array
(
'datetime_in' => '2016-02-25 02:23:00'
)
),
3 => Array
(
'DailyCallReport' => Array
(
'datetime_in' => '2016-02-25 04:00:00'
)
)
);
$date_in = '';
foreach ($dcr_all as $key=>$d):
if($date_in != $d['DailyCallReport']['datetime_in']){
if($date_in == ''){
$date_in = $d['DailyCallReport']['datetime_in'];
}
else{
$current_row_date = $d['DailyCallReport']['datetime_in'];
if(strtotime($current_row_date) - (strtotime($date_in)+3600) >= 3600){
$date_in = $current_row_date;
echo '<br/>';
}
}?>
<tr>
<td><?php echo $key+1?></td>
<td >
<?php
$date_in = new DateTime($date_in);
echo $date_in = $date_in -> format('h:i a');
?>
</td>
</tr>
<?php
}
endforeach;
?>
这是小提琴link
答案 1 :(得分:0)
您只需跟踪上次日期时间。因此,当您回显它时,不要用格式覆盖它。然后在每次迭代时做一个日期差异。
<?php $date_in = null; ?>
<?php foreach ($dcr_all as $key=>$d):?>
<?php
if ($date_in && $date_in->diff(new DateTime($d['DailyCallReport']['datetime_in']))->format('%h') >= 1) {
echo "<tr><td></td><td>Message here</td></tr>";
}
?>
<tr>
<td><?php echo $key+1?></td>
<td >
<?php $date_in = $d['DailyCallReport']['datetime_in'];
$date_in = new DateTime($date_in);
//don't overwrite the $date_in with the format here
echo $date_in->format('h:i a');
?>
</td>
</tr>
<?php endforeach;?>
答案 2 :(得分:0)
这是一个简单的解决方案。只需用当前脚本替换你的脚本,你就会得到一个空行。
<?php
$lastRowDateTime = null;
foreach ($dcr_all as $key=>$d):
$date_in = $d['DailyCallReport']['datetime_in'];
if (!is_null($lastRowDateTime) && strtotime($date_in) > strtotime($lastRowDateTime)+3600) {
?>
<tr><td>your message goes here</td></tr>
<?php } ?>
<tr>
<td><?php echo $key+1?></td>
<td>
<?php
$date_in = $d['DailyCallReport']['datetime_in'];
$date_in = new DateTime($date_in);
echo $date_in = $date_in -> format('h:i a');
?>
</td>
</tr>
<?php
$lastRowDateTime = $d['DailyCallReport']['datetime_in'];
endforeach;
?>