Add date with bank values. CakePHP 3.0

2

I need to make a calculation that adds an integer to a date. This whole number 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 do not know what's wrong, I searched the entire internet and that's what they say to do to add dates. Any help?

    
asked by anonymous 25.01.2016 / 12:41

1 answer

2

You can use the add() method of the DateTime class, probably the cake uses it by default to manipulate dates, it simply passes an instance of the desired period, be it days, months, years, etc.

The problem with your code is to format the date and then the manipulation, the operations by default are done in Y-m-d format 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);
    
25.01.2016 / 14:18