php - Add the correct number of days for each month -
using following function can display years , months between 2 dates, how can add correct days each month array within each month? can't add days manually need account leap years etc.
function yearmonth($start_date, $end_date) { $begin = new datetime( $start_date ); $end = new datetime( $end_date); $interval = new dateinterval('p1m'); // 1 month interval $period = new dateperiod($begin, $interval, $end); foreach ( $period $dt ) $years[$dt->format( "y" )][] = $dt->format( "f" ); return $years; } $list = yearmonth("2007-03-24", "2009-06-26"); var_dump($list);
since nobody else answered:
function yearmonth($start_date, $end_date) { $begin = new datetime( $start_date ); $end = new datetime( $end_date); $interval = new dateinterval('p1d'); // 1 month interval $period = new dateperiod($begin, $interval, $end); $lastmonth = null; $lastyear = null; $aresult = array(); foreach ( $period $dt ) { if ($dt->format('y') != $lastyear) { $lastyear = $dt->format('y'); } if ($dt->format('f') != $lastmonth) { $lastmonth = $dt->format('f'); } if (!isset($aresult[$lastyear])) { $aresult[$lastyear] = array(); } if (!isset($aresult[$lastyear][$lastmonth])) { $aresult[$lastyear][$lastmonth] = array(); } $aresult[$lastyear][$lastmonth][] = $dt->format('d'); } return $aresult; }
on side note planing create sort of gantt chart style flat layout in table format of years, months , days between dates. think suitable way of generating that? or there better way?
Comments
Post a Comment