Php 如何计算同月数组数
如果上面给出了数组 我正在做一份报告,每月监测用户增长情况。。我想统计每个月的参赛人数 样本4月2日计数Php 如何计算同月数组数,php,mysql,Php,Mysql,如果上面给出了数组 我正在做一份报告,每月监测用户增长情况。。我想统计每个月的参赛人数 样本4月2日计数 $dates = array('2017-03-24 01:48:09', '2017-03-24 11:48:09', '2017-04-07 01:12:19', '2017-04-14 01:49:09', '2017-04-21 01:45:09', '2017-04-28 01:38:09'); 输出将是: $dates = array('2017-03-24 01:48:09'
$dates = array('2017-03-24 01:48:09', '2017-03-24 11:48:09', '2017-04-07 01:12:19', '2017-04-14 01:49:09', '2017-04-21 01:45:09', '2017-04-28 01:38:09');
输出将是:
$dates = array('2017-03-24 01:48:09', '2017-03-24 11:48:09', '2017-04-07 01:12:19', '2017-04-14 01:49:09', '2017-04-21 01:45:09', '2017-04-28 01:38:09');
$count = array();
foreach ($dates as $d) {
$count[date('m', strtotime($d))]++;
}
print_r($count);
$count[date('F', strtotime($d))]++;
也就是说:03
(三月)在给定数组中有2个值
如果您需要将03
作为March
,请使用:
Array ([03] => 2 [04] => 4 )
然后,输出将是:
$dates = array('2017-03-24 01:48:09', '2017-03-24 11:48:09', '2017-04-07 01:12:19', '2017-04-14 01:49:09', '2017-04-21 01:45:09', '2017-04-28 01:38:09');
$count = array();
foreach ($dates as $d) {
$count[date('m', strtotime($d))]++;
}
print_r($count);
$count[date('F', strtotime($d))]++;
2017年1月和2018年1月会是同一个数字吗?或者这是假设数组中的条目具有相同的年份?…并且这些数据已经在MySQL表中了吗?到目前为止您做了什么?OP中的代码段需要使用substr()。但是,如果您告诉我们更多,您可能会得到更好的响应。
array (
0 => '2017-03',
1 => '2017-03',
2 => '2017-04',
3 => '2017-04',
4 => '2017-04',
5 => '2017-04',
)array (
'2017-03' => 2,
'2017-04' => 4,
)