To always catch the last date of the month use date
with mktime()
:
date('t', mktime());
Generate plots by the last day of the month according to the information of the year:
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 is generated as if it were instalments of payment, that is to say, whether 13/12/2005 and the amount of plots the algorithm generate from that date only changing the month and year if it goes to the next. This code also fits your doubt if you are informed the last date of the month with 12 plots.
Generate plots by 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 the same is in the question always has to pass the last day of the first month of the year, because, this code generates for any day of the month other than the first that takes the year and generates the last date of the month of that year.
References:
http://answall.com/a/38633/91
– rray
Ok interesting o’t'.... more i how to do it in sequence
– Fabio Henrique
Try something like that inside your
for
:echo date('Y-m-t', strtotime("2017-" . ($i < 10 ? "0$i" : $i) . "-01")) . "\n";
– rdleal