Created
May 15, 2014 03:19
-
-
Save axgle/cf7a3938773616216087 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function year_by_month($start=null,$end=null) { | |
$data=array(); | |
$start=strtotime($start); | |
if($end===null){ | |
$end=date("Y-m-d"); | |
} | |
$end=strtotime($end); | |
$m_start=date("Y",$start)*12+(int)date("m",$start); | |
$m_end=date("Y",$end)*12+(int)date("m",$end); | |
while ($m_start<=$m_end){ | |
$m=$m_start%12; | |
$y=($m_start-$m)/12; | |
if($m==0){ | |
$m=12; | |
$y--; | |
} | |
$days=cal_days_in_month(CAL_GREGORIAN, $m, $y); | |
$data[]=array('y'=>$y,'m'=>$m,'days'=>$days); | |
$m_start++; | |
} | |
$info=array(); | |
foreach($data as $d){ | |
$y=$d['y']; | |
if(!isset($info[$y])){ | |
$info[$y]=array(); | |
} | |
$d['m']= sprintf("%02d", $d['m']); | |
$info[$y][]=array($d['y'].'-'.$d['m'].'-01',$d['y'].'-'.$d['m'].'-'.$d['days']); | |
} | |
$info[date("Y",$start)][0][0]=date("Y-m-d",$start); | |
$info[date("Y",$end)][count($info[date("Y",$end)])-1][1]=date("Y-m-d",$end); | |
$d=array(); | |
foreach($info as $y=>$rows){ | |
if(!isset($d[$y])){ | |
$d[$y]=array(); | |
} | |
foreach($rows as $row){ | |
$m=(int)date("m",strtotime($row[0])); | |
$d[$y][$m]=$row; | |
} | |
} | |
return ($d); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment