I called your variables $dia
, $mes
and $ano
for easy reading.
If you want to know if the date is valid, just use the checkdate
:
checkdate( $mes, $dia, $ano ) // Atenção à ordem dos parâmetros.
To see if the date is today or earlier:
if ( mktime( 0, 0, 0, $mes, $dia, $ano ) < time() ) // não precisa de <= (a hora é 0, 0, 0)
To see if the year is greater than or equal to 1900:
if ( $ano >= 1900 )
Putting it all together and reversing the checks:
if ( !checkdate( $mes , $dia , $ano ) // se a data for inválida
|| $ano < 1900 // ou o ano menor que 1900
|| mktime( 0, 0, 0, $mes, $dia, $ano ) > time() ) // ou a data passar de hoje
{
echo 'Data inválida';
}
If it makes a difference the "turn" of the date at midnight:
The functions used above are based on the server time. If you need more accuracy, an alternative is to use the class DateTime
, which accepts comparisons from the PHP 5.2.2, and in addition to date_default_timezone_set
, Timezone can be set individually in each of the DateTime
s, so as not to interfere with other parts of the script.
date_default_timezone_set('America/Sao_Paulo');
$hoje = new DateTime('NOW');
$nascimento = new DateTime();
$nascimento->setDate( $ano, $mes, $dia ); // Novamente, atenção à ordem dos parâmetros.
if ( $nascimento > $hoje ) || ... etc ...
1900 to present date
– Alan PS
What do you mean by "pulling from the bd"? Do you have invalid data in the database and want to validate by php? Or do you want to validate before arriving at the database?
– bfavaretto