How do I get get list of month/year serially starting from the current month/year. i.e. jan 2017, feb 2017, march 2017, april 2017 and so on. The list could be of any length (jan to oct, jan to dec, etc).
P.S. There are no start and end date range.
How do I get get list of month/year serially starting from the current month/year. i.e. jan 2017, feb 2017, march 2017, april 2017 and so on. The list could be of any length (jan to oct, jan to dec, etc).
P.S. There are no start and end date range.
You can create a function to generate this list by passing the length. Below is a sample code that might help you.
In this example 1=jan and 12=dec
$list = getList(01,02);
print_r($list);
function getList($from,$to)
{
$res = array();
for($i=$from;$i<=$to;$i++)
{
$res[] = date('M Y',strtotime('2017'.'-'.$i));
}
return $res;
}
This is a possible solution, using intervals:
function getList($n) {
$now=new DateTime();
$myDate=new DateTime($now->format('Y-m-1'));
$interval=new DateInterval('P1M');
$result=[];
for ($i=0; $i<$n; ++$i) {
$result[]=$myDate->format('M Y');
$myDate->add($interval);
}
return $result;
}
It creates a date pointing to the first day in the current month, and after keeps adding one month to it at each cycle.