Comparison of dates

0

I want the message to appear when the user enters no date or the start date is greater than the end date. The code I made was this:

/**
 * @Assert\True(message = "Erro! Verifique as horas. Data inical não pode ser maior que a data final.")
 */
public function isHoraFim() 
{
    if ($this->horainicial > $this->horafim) 
    {
        return false;
    } 
    else 
    {
        return true;
    }

    if ($this->horainicial || $this->horafim) 
    {
        return false;
    }    

    return $this->horainicio < $this->horafim;
}

But the error message always appears, except when the user enters no date.

    
asked by anonymous 11.02.2015 / 16:02

2 answers

1

I leave here the resolution there is my question for anyone who has the same doubt as me. What happened to me was to have called the same name as my variable the program was considering it as variable so it was to be confused.

 /**
 * @Assert\True(message = "Erro! Verifique as horas. Data inical não pode ser maior que a data final.")
 */
public function isHorasCheck() {
        return ($this->horainicio < $this->horafim);
}

What I added, but that was not in the question, I thought it would look better.

 /**
 * @Assert\True(message = "Erro! Verifique as horas. Data inical não pode ser maior que a data final.")
 */
public function isHorasCheck() {

    if ($this->horafim === null){
        return true;
    }
    if ($this->horainicio === null){
        return false;
    }
    else{
        return ($this->horainicio < $this->horafim);
    }

}

One way or another works now is just to see what's best for you.

    
11.02.2015 / 18:40
0

If you need to return true or false, a simple line resolves:

/**
 * @Assert\False(message = "Erro! Verifique as horas. Data inicial não pode ser maior que a data final.")
 */
public function isHoraFim() 
{
    return (!$this->horainicial || !$this->horafim || $this->horainicial > $this->horafim);
}

Or:

/**
 * @Assert\True(message = "Erro! Verifique as horas. Data inicial não pode ser maior que a data final.")
 */
public function isHoraFim() 
{
    return ($this->horainicial && $this->horafim && $this->horainicial < $this->horafim);
}
    
11.02.2015 / 16:07