example - ideone
$date1 = "12/06/2012";
$date1 = str_replace("/", "-", $date1);
// data americana
$dateInicio = date('Y-m-d', strtotime($date1));
$date2 = "12/06/2017";
$date2 = str_replace("/", "-", $date2);
// data americana
$dateFim = date('Y-m-d', strtotime($date2));
$time1=strtotime($date1);
// ano data inicial
$year1=date("Y",$time1);
$time2=strtotime($date2);
// ano data final
$year2=date("Y",$time2);
$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 "<br>";
// 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 "<br>";
}
// 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 <br>";
}
}
If we use this format of dd-mm-yyyy
we will have errors, so we use the function date()
which returns PHP dates in the format you want. date()
$interval->format - Formats a range.
The "Unix Era" began on January 1, 1970, and thanks to it, we can perform precise calculations of date. The function mktime() returns the total of seconds that have passed since the beginning of the Unix Age. The function date() can format dates based on the Unix era!
As the questioner comments
é que preciso incluir o dia final
simply include in the code the following line
$dateFim = date('Y-m-d', strtotime($dateFim. ' + 1 days'));
Then we will have as a result in the ideone
$dateFim ="";
$date1 = "12/06/2012";
$date1 = str_replace("/", "-", $date1);
// data americana
$dateInicio = date('Y-m-d', strtotime($date1));
$date2 = "12/06/2017";
$date2 = str_replace("/", "-", $date2);
// data americana
$dateFim = date('Y-m-d', strtotime($date2));
$dateFim = date('Y-m-d', strtotime($dateFim. ' + 1 days'));
$time1=strtotime($date1);
// ano data inicial
$year1=date("Y",$time1);
$time2=strtotime($date2);
// ano data final
$year2=date("Y",$time2);
$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 "<br>";
// 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 "<br>";
}
// 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 <br>";
}
}
but from what I understand you only need to calculate the period of the first and last year, the others will be all 12 months, no?
– Mateus
if it is in days you have to calculate all periods
– user60252
But by what he presented as correct output would be in months, so it does not matter if a month had 28 or 31 days, closed is month and a year 12 months
– Mateus
has waste in days
– user60252
But only in the first and last, so I would only need the
12-01-2012 - 01-01-2012
and of12-06-2017 - 01-01-2017
the others would be 12 full months with no residue– Mateus
this exit for 2012 is not right, 2012 = 5 months and 12 days, see that from JULY to the end of the year are already 6 months and still have the days of June
– user60252
I don’t know if you could use some lib, but this would help you a lot: https://github.com/briannesbitt/Carbon
– Luiz Pillon
Related: https://answall.com/q/70473/132
– Victor Stafusa