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
这两个功能单独写。请大侠赐教,谢谢!!!!!!!!!
------解决方案--------------------
$ar = array(<br /> '2014-01-01',<br /> '2014-01-02',<br /> '2014-01-22',<br /> '2014-02-22',<br /> '2014-02-26',<br /> '2014-03-03',<br /> '2014-03-25',<br /> '2014-03-28',<br />);<br />$r1 = $r2 = array();<br />foreach($ar as $d) {<br /> list($y, $m, $d) = explode('-', $d);<br /> if(! isset($r1[$y.$m])) $r1[$y.$m] = 32;<br /> $r1[$y.$m] = min($r1[$y.$m], $d);<br /> if(! isset($r2[$y.$m])) $r2[$y.$m] = 0;<br /> $r2[$y.$m]++;<br />}<br />print_r($r1);<br />print_r($r2);<br />
$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');<br />sort($arr);<br />$out = array();<br />foreach ($arr as $key => $value) {<br /> $mouth = date('n',strtotime($value));<br /> if(!in_array($mouth, array_keys($out))) $out[$mouth] = $value;<br />}<br />var_dump($out);
$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');<br />$newArr = array_map('cout', $arr);<br />function cout($n){<br /> return date('n',strtotime($n));<br />}<br />print_r ( array_count_values ( $newArr ));
<br /><?php<br />$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');<br /><br />month_min_day($months);<br /><br />month_days($months);<br /><br />function month_min_day($months){<br /> $tmp = array();<br /> foreach($months as $month){<br /> $key = date('Y-m',strtotime($month));<br /> if(!isset($tmp[$key]) <br><font color='#FF8000'>------解决方案--------------------</font><br> strtotime($month)<$tmp[$key]){<br /> $tmp[$key] = strtotime($month);<br /> }<br /> }<br /> foreach($tmp as $key=>$val){<br /> echo date('Y-m-d',$val).'<br>';<br /> }<br />}<br /><br />function month_days($months){<br /> $tmp = array();<br /> foreach($months as $month){<br /> $key = date('Y-m',strtotime($month));<br /> isset($tmp[$key])? $tmp[$key]++ : $tmp[$key]=1;<br /> }<br /> foreach($tmp as $key=>$val){<br /> echo $key.'->'.$val.'<br>';<br /> }<br />}<br />?><br />