<?php
$date1 = "12/06/2012";
// data americana
$date2 = "12/06/2017";
// data americana
// ano data inicial
// ano data final
$difAno=$year2-$year1;
// se a diferença em anos for maior que 0 calculamos ano inicial e final
if ($difAno>0){
// para ano inicial
$fim = ($year1."-12-31");
$datetime1 = new DateTime($dateInicio);
$datetime2 = new DateTime($fim);
$interval = $datetime1->diff($datetime2);
echo "Ano: ".($year1)." - ";
echo $interval->format('%m Meses %d dias');
echo "\n";
// para ano final
$inicio = ($year2."-01-01");
$datetime1 = new DateTime($inicio);
$datetime2 = new DateTime($dateFim);
$interval = $datetime2->diff($datetime1);
echo "Ano: ".($year2)." - ";
echo $interval->format('%m Meses %d dias');
echo "\n";
}
// se a diferença entre os anos for maior que 1 fazemos um loop para calcular os demais
if ($difAno>1){
for ($x = 1; $x <= $difAno-1; $x++) {
echo "Ano: ".($year1+$x)." - ";
echo date("z", mktime(0,0,0,12,31,($year1+$x))) + 1; echo " = 12 meses \n";
}
}
PD9waHAKCiRkYXRlMSA9ICIxMi8wNi8yMDEyIjsKJGRhdGUxID0gc3RyX3JlcGxhY2UoIi8iLCAiLSIsICRkYXRlMSk7Ci8vIGRhdGEgYW1lcmljYW5hCiRkYXRlSW5pY2lvID0gZGF0ZSgnWS1tLWQnLCBzdHJ0b3RpbWUoJGRhdGUxKSk7CgokZGF0ZTIgPSAiMTIvMDYvMjAxNyI7CiRkYXRlMiA9IHN0cl9yZXBsYWNlKCIvIiwgIi0iLCAkZGF0ZTIpOwovLyBkYXRhIGFtZXJpY2FuYQokZGF0ZUZpbSA9ICBkYXRlKCdZLW0tZCcsIHN0cnRvdGltZSgkZGF0ZTIpKTsKCiR0aW1lMT1zdHJ0b3RpbWUoJGRhdGUxKTsKLy8gYW5vIGRhdGEgaW5pY2lhbAokeWVhcjE9ZGF0ZSgiWSIsJHRpbWUxKTsKCiR0aW1lMj1zdHJ0b3RpbWUoJGRhdGUyKTsKLy8gYW5vIGRhdGEgZmluYWwKJHllYXIyPWRhdGUoIlkiLCR0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