To always catch the last date of the month use date
with mktime()
:
date('t', mktime());
Generate installments by the last day of the month according to the year's information:
function gerar_parcelas($y = 2017)
{
$parc = array();
for($i = 1; $i < 13;$i++){
$parc[] = date('Y-m-t', mktime(0,0,0,$i,1,(int)$y));
}
return $parc;
}
var_dump(gerar_parcelas(2017));
Example
In this other code it is generated as if it were payment parcels , that is, if it was informed 12/13/2005 and the number of parcels the algorithm generates from this date only changing the month and year if it goes to the next. This code also fits your question if you enter the last date of the month with 12 plots.
Generate installments for a date:
<?php
function parcelas($data, $numero = 12)
{
$parc = array();
$parc[] = $data;
list($ano, $mes, $dia) = explode("-", $data);
for($i = 1; $i < $numero;$i++)
{
$mes++;
if ((int)$mes == 13)
{
$ano++;
$mes = 1;
}
$tira = $dia;
while (!checkdate($mes, $tira, $ano))
{
$tira--;
}
$parc[] = sprintf("%02d-%02d-%02d", $ano, $mes, $tira);
}
return $parc;
}
$data = "2017-01-31";
var_dump(parcelas($data, 12));
Example
In this second example is generated by the date so to work equal is in the question always have to go through the last day of the first month of the year because, this code generates for any day of the month different from the first that takes the year and generates the last date of the month of that year.
References: