Compare Dates - PHP - Codeigniter

Asked

Viewed 491 times

1

Hello, I have the following case I’m with a date in the database saving so: (02-12-2016). The idea would be to take the current date and if it is less than 2 days that the date quoted above does such a thing. I tried to do so:

        foreach ($teste as $row) {
        $data_validade = $row->data_validade;
        echo $data_validade;
        $data_atual = date('d-m-Y');
        $diferenca = strtotime($data_validade) - strtotime($data_atual);
        $dias = floor($diferenca / (60 * 60 * 24));

        if ($dias < 2) {
            redirect('Painel_voluntario/index/?aviso=4');
        } else {
            $dados['status_vaga'] = 'Voluntário Desistiu...';
            $data = array(
                "excluir" => $this->Vaga_model->aceitarOuRecusar($id_vaga, $id_voluntario, $dados)
            );
            redirect('Painel_voluntario/index/?aviso=3');
        }

    }

In this case it is even working, but for example if I type a very high date as 29-10-2998, won’t work(is falling in if it is less than two days, which soon is wrong).

1 answer

1


Try this:

foreach ($teste as $row) {
    $data_validade = date('Y-m-d', strtotime($row->data_validade));
    echo $data_validade;
    $data_atual = new DateTime(date('Y-m-d'));
    $diferenca = $data_validade->diff($data_atual);

    $dias = $diferenca->format('%a');
    $op = $diferenca->format('%R');

    if ($op == "-" && $dias > 2) {
        redirect('Painel_voluntario/index/?aviso=4');
    } else {
        $dados['status_vaga'] = 'Voluntário Desistiu...';
        $data = array(
            "excluir" => $this->Vaga_model->aceitarOuRecusar($id_vaga, $id_voluntario, $dados)
        );
        redirect('Painel_voluntario/index/?aviso=3');
    }

}

Browser other questions tagged

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