Sum data with database values. Cakephp 3.0

Asked

Viewed 177 times

2

I need to do a calculation that sums an integer to a date. This integer refers to days.

$user = TableRegistry::get('PoliticaSenha');

$query = $user->find()->where(['id' => $usuario['politica_senha_id']])->first();

$dataTrocaUsuario = $usuario->ultima_troca_senha->format("d/m/Y");

$dataValidade = date('d/m/Y', strtotime("+".$query->validade_dias ." days", strtotime($dataTrocaUsuario)));

        echo $query->validade_dias;
        echo ' - ';
        echo $dataValidade;
        echo ' - ';
        echo $dataTrocaUsuario;

this is my result:

2 - 02/01/1970 - 22/01/2016 

I don’t know what’s wrong, I looked all over the internet and that’s what they say to do to add dates. Some help ?

  • validade_dia of days is the whole number?

  • That’s right, as you can see from the echo result.

1 answer

2

You can use the method add() of the Datetime class, probably the cake uses it by default to manipulate dates, for it just to pass an instance of the desired period, whether in days, months, years etc.

The problem with your code is formatting the date and then manipulating, the default operations are done in the format Y-m-d when you send in another format an invalid date is generated.

$query = $user->find()->where(['id' => $usuario['politica_senha_id']])->first();

$dataTrocaUsuario = $usuario->ultima_troca_senha;
$dataValidade = $dataTrocaUsuario
dataTrocaUsuario->add(new DateInterval("P{$query->validade_dias}D"));

echo $query->validade_dias;
echo ' - ';
echo $dataValidade->format('d/m/Y);
echo ' - ';
echo $dataTrocaUsuario->format('d/m/Y);

Browser other questions tagged

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