Php determines the minimum date of a month, for example, the date in an array: 2014-01-01,2014-01-02,2014-01-22,2014-02-22,2014-02-26,2014-03-03,2014-03-25 ,2014-03-28 must reach: use month php to determine the minimum date of a month
For example, the date in an array: 2014-01-01,2014-01-02,2014-01-22,2014-02-22,2014-02-26,2014-03-03,2014-03-25 ,2014-03-28
To reach: obtain the minimum date in the month, as shown below:
2014-01-01
2014-02-22
2014-03-03
And calculate the number of days of the month: (for example, there are 3 in February (2014-01-01,2014-01-02,2014-01-22 ))
September-"3
September-"2
September-"3
These two functions are written separately. Thank you !!!!!!!!!
------ Solution --------------------
$ar = array(
'2014-01-01',
'2014-01-02',
'2014-01-22',
'2014-02-22',
'2014-02-26',
'2014-03-03',
'2014-03-25',
'2014-03-28',
);
$r1 = $r2 = array();
foreach($ar as $d) {
list($y, $m, $d) = explode('-', $d);
if(! isset($r1[$y.$m])) $r1[$y.$m] = 32;
$r1[$y.$m] = min($r1[$y.$m], $d);
if(! isset($r2[$y.$m])) $r2[$y.$m] = 0;
$r2[$y.$m]++;
}
print_r($r1);
print_r($r2);
Array
(
[201401] => 01
[201402] => 22
[201403] => 03
)
Array
(
[201401] => 3
[201402] => 2
[201403] => 3
)
------ Solution --------------------
1.
$arr = array('2014-01-01','2014-01-02','2014-01-22','2014-02-22','2014-02-26','2014-03-03','2014-03-25','2014-03-28');
sort($arr);
$out = array();
foreach ($arr as $key => $value) {
$mouth = date('n',strtotime($value));
if(!in_array($mouth, array_keys($out))) $out[$mouth] = $value;
}
var_dump($out);
2.
$arr = array('2014-01-01','2014-01-02','2014-01-22','2014-02-22','2014-02-26','2014-03-03','2014-03-25','2014-03-28');
$newArr = array_map('cout', $arr);
function cout($n){
return date('n',strtotime($n));
}
print_r ( array_count_values ( $newArr ));
------ Solution --------------------
If you want to consider the same month in different years, for example, 2014-02 and 2013-02, you need to separate statistics. The key value must be in Y-m format.
$ Months = array ('2017-01-01 ', '2017-01-02', '2017-01-22 ', '2017-02-22', '2017-02-26 ', '2017-03-03 ', '2017-03-25', '2017-03-28 ');
Month_min_day ($ months );
Month_days ($ months );
Function month_min_day ($ months ){
$ Tmp = array ();
Foreach ($ months as $ month ){
$ Key = date ('Y-M', strtotime ($ month ));
If (! Isset ($ tmp [$ key])
------ Solution --------------------
Strtotime ($ month) <$ tmp [$ key]) {
$ Tmp [$ key] = strtotime ($ month );
}
}
Foreach ($ tmp as $ key => $ val ){
Echo date ('Y-m-D', $ val ).'
';
}
}
Function month_days ($ months ){
$ Tmp = array ();
Foreach ($ months as $ month ){
$ Key = date ('Y-M', strtotime ($ month ));
Isset ($ tmp [$ key])? $ Tmp [$ key] ++: $ tmp [$ key] = 1;
}
Foreach ($ tmp as $ key => $ val ){
Echo $ key. '->'. $ val .'
';
}
}
?>