我想显示每月的带宽使用情况,
有2位用户均于6月登录
这是我的代码:
<?php foreach ($data as $row):
$date = $row['acctstarttime'];
$d = date_parse_from_format("Y-m-d", $date);
$monthNum = $d["month"];
$dateObj = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F');
?>
<td><?php echo $monthName;?></td>
</tr>
<?php endforeach;?>
输出是:
- 6月
- 六月
- 7月号
我需要的输出:
- 6月
- 7月
我尝试将查询更改为:
Function showData()
{
$this->db->distinct('radacct.acctstarttime');
$this->db->from('radacct');
$this->db->where('radacct.acctstarttime','y-06-d-h');
$query = $this->db->get();
If ($query->num_rows()>0)
{
Return $query->result();
}
Else
{
Return array();
}
}
但它什么都没改变(我的查询错了吗?)
我也尝试过:foreach (array_unique($data) as $row)
但它删除了7月份
<?php print_r($row['acctstarttime'])?>
的输出
是:2017-06-08 04:12:00 2017-06-08 04:12:00 2017-07-12 05:00:00
答案 0 :(得分:2)
试试这个:
<?php
$month = [];
foreach ($data as $row):
if(!in_array($d["month"], $month)) {
$month[] = $d['month'];
$date = $row['acctstarttime'];
$d = date_parse_from_format("Y-m-d", $date);
$monthNum = $d["month"];
$dateObj = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F');
?>
<td><?php echo $monthName;?></td>
</tr>
<?php
}
endforeach;
?>
<强>更新强>
$month = array();
foreach ($data as $row):
$date = $row['acctstarttime'];
$d = date_parse_from_format("Y-m-d", $date);
$monthNum = $d["month"];
$dateObj = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F');
if(!in_array($monthName, $month)) {
$month[] = $monthName;
echo $monthName;
}
endforeach;