Calculate the difference of months

Asked

Viewed 47 times

-1

I have that function

<?php
    function calcula_intervalo_custo($data_inicial, $data_final) {

        function geraTimestamp($data) {
            $partes = explode('/', $data);
            return mktime(0, 0, 0, $partes[1], $partes[0], $partes[2]);
        }


        $time_inicial = geraTimestamp($data_inicial);
        $time_final   = geraTimestamp($data_final);


        $diferenca = $time_final - $time_inicial; 


        $res_mes = (int)floor( $diferenca / (60 * 60 * 24 * 24)); 

        return $res_mes;
    }
?>

That returns the difference between months the function says that of 01/09/2019 for 01/02/2019 the difference is 6 months (the correct would be 5 months), and that of 01/12/2019 for 01/02/2019 is of 2 months (this is right).

  • Use the class DateTime

  • What exactly does the 60 value mean6024*24?

  • the answers you left in the question are not wrong ... the differences are not those !!!

1 answer

1


I got the error:

PHP Fatal error: Cannot redeclare geraTimestamp()

This is clear because if you use more than once the function it will call the statement again every time you use the function, which is solved like this:

function geraTimestamp($data) {
    $partes = explode('/', $data);
    return mktime(0, 0, 0, $partes[1], $partes[0], $partes[2]);
}

function calcula_intervalo_custo($data_inicial, $data_final) {
    $time_inicial = geraTimestamp($data_inicial);
    $time_final   = geraTimestamp($data_final);


    $diferenca = $time_final - $time_inicial; 


    $res_mes = (int)floor( $diferenca / (60 * 60 * 24 * 24)); 

    return $res_mes;
}

Then after fixed while running this:

var_dump(calcula_intervalo_custo('01/02/2019', '01/09/2019')); //Obtive 8
var_dump(calcula_intervalo_custo('01/02/2019', '01/12/2019')); //Obtive 12

These are totally different values from the ones you mentioned, but maybe I wonder why, is that actually it is not 01/09/2019 and 01/12/2019, the right one would be the year 2020 for both, then:

var_dump(calcula_intervalo_custo('01/09/2019', '01/02/2020')); //6
var_dump(calcula_intervalo_custo('01/12/2019', '01/02/2020')); //2
  • From the 1st to the 30th of September you have 30 days
  • From 1 to 31 November has 31 days
  • From the 1st to the 31st of December you have 31 days
  • From the 1st to the 31st of January has 31 days
  • From the 1st to the 31st of February it has 29 days (leap year)

Adding up we have 152 days, divided by 30 would 5,06, round to 5, then what seems wrong to me is this part:

60 * 60 * 24 * 24

If 60 is seconds, 60 is minutes, 24 is hours, then the other 24 must be days, but a month has an average 30 days and not 24, I believe the correct would be:

60 * 60 * 24 * 30

If your goal is to discount the final days of weeks and Sundays then I guess it doesn’t make sense because the days have passed anyway and in fact you would have to discount first the difference value and then calculate, that’s a hypothetical code:

$diferenca = $time_final - $time_inicial - $finais_de_semana;

$res_mes = (int) floor( $diferenca / (60 * 60 * 24 * 30)); 

But I’m not sure if it’s what you want, but if it’s the case maybe to remove the Saturday days and domigo maybe you have to create a loop and check day by day with a IF:

if (date('N', $dia) < 6) {
   //É dia de semana
}

I’m not going to suggest a script or anything, because you might have answers on the site that already solve, but I have no way of knowing if this is your problem, so far as it seems you’ve only confused the 60 * 60 * 24 * 24 when it should be 60 * 60 * 24 * 30

Browser other questions tagged

You are not signed in. Login or sign up in order to post.