php 判断月度中最小的日期
程序员文章站
2022-06-03 11:09:49
...
php 判断月份中最小的日期
比如:一个数组里边装的日期:2014-01-01,2014-01-02,2014-01-22,2014-02-22,2014-02-26,2014-03-03,2014-03-25,2014-03-28
要达到:把月份中最小的日期取出来,如下所示:
2014-01-01
2014-02-22
2014-03-03
并且计算月份所属日期数:(比如1月份有3个(2014-01-01,2014-01-02,2014-01-22))
01月份-》3
02月份-》2
03月份-》3
这两个功能单独写。请大侠赐教,谢谢!!!!!!!!!
------解决方案--------------------
(
[201401] => 01
[201402] => 22
[201403] => 03
)
Array
(
[201401] => 3
[201402] => 2
[201403] => 3
)
------解决方案--------------------
1.
2.
------解决方案--------------------
如果要考虑不同年相同月份的情况,例如2014-02 与 2013-02 需要分开统计。key的值要用Y-m格式。
比如:一个数组里边装的日期:2014-01-01,2014-01-02,2014-01-22,2014-02-22,2014-02-26,2014-03-03,2014-03-25,2014-03-28
要达到:把月份中最小的日期取出来,如下所示:
2014-01-01
2014-02-22
2014-03-03
并且计算月份所属日期数:(比如1月份有3个(2014-01-01,2014-01-02,2014-01-22))
01月份-》3
02月份-》2
03月份-》3
这两个功能单独写。请大侠赐教,谢谢!!!!!!!!!
------解决方案--------------------
$ar = array(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);
(
[201401] => 01
[201402] => 22
[201403] => 03
)
Array
(
[201401] => 3
[201402] => 2
[201403] => 3
)
------解决方案--------------------
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 ));
------解决方案--------------------
如果要考虑不同年相同月份的情况,例如2014-02 与 2013-02 需要分开统计。key的值要用Y-m格式。
$months = 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');
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])
------解决方案--------------------
strtotime($month) $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.'
';
}
}
?>
相关文章
相关视频
上一篇: PHP的输出缓冲区_PHP教程