我试图获取按月注册的用户数量,以将其插入到chart.js中。我遇到的问题是显示每月的计数结果。
我从mysql查询中得到以下结果
php print_r($this->results);
结果
Array (
[0] => Array
(
[d] => 2018-05-15 21:54:08
)
[1] => Array
(
[d] => 2018-05-16 15:50:58
)
[2] => Array
(
[d] => 2018-06-18 18:27:11
)
)
答案应该是:05 = 2和06 = 1
感谢您的帮助!
答案 0 :(得分:1)
代码
$data = [];
$data[0]['d'] = '2018-05-15 21:54:08';
$data[1]['d'] = '2018-05-16 15:50:58';
$data[2]['d'] = '2018-06-18 18:27:11';
$result = [];
foreach( $data as $element ) {
// convert date to timestramp by strToTime
// get month via date( 'm' )
$month = date( 'm', strToTime( $element['d'] ) );
// if a count for month is present increment it
if( isset( $result[$month] )) {
$result[$month]++;
} else { // if NO count is present, create and set to 1
$result[$month] = 1;
}
}
echo '<pre>';
var_dump($result);
结果
array(2) {
["05"]=>
int(2)
["06"]=>
int(1)
}
修改
添加了说明