Add value to a reference value determined each different day for the whole year

Asked

Viewed 26 times

1

How can I add a certain value to an existing value each day other than the year. Something like:

$hoje =  date('Y-m-d');                     
$amanha = date('Y-m-d', strtotime($hoje .' +1 day'));

if ($hoje  != $amanha) {
    somar 40 a 19000
}
//hoje imprime 19040, no dia seguinte imprime 19080 e assim por diante até o fim do ano.

1 answer

1


The example below calculates the difference in days between a predetermined date and the current date, and after that, adds up the values as you wished.

<?php
// Constantes
$DATA_INICIAL = "2019-02-10";
$DATA_ATUAL = time();
$VALOR_INICIAL = 19000;
$VALOR_A_SOMAR = 10;

// Faz a diferença entre as datas desejadas e converte para dias
$diferencaEntreDatasEmMilissegundos = $DATA_ATUAL - strtotime($DATA_INICIAL);
$diferencaEmDias = intval($diferencaEntreDatasEmMilissegundos/86400);

// Efetua a soma desejada
$valorFinal = $VALOR_INICIAL + ($diferencaEmDias * $VALOR_A_SOMAR);

See it working on ideone: https://ideone.com/4qFIiB

  • It worked well, although I have made some modifications, thank you John !

Browser other questions tagged

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