Php - Validar Fechas


/ Published in: PHP
Save to your folder(s)

echo validarFecha( '1-1-1998', 0 ) ? 'ok' : 'ko' ; #ok
echo validarFecha( '1-1-1998' ) ? 'ok' : 'ko' ; #ko
echo validarFecha( '1980/05/06' ) ? 'ok' : 'ko' ; #ok
echo validarFecha( '1700-5-1' ) ? 'ok' : 'ko' ; #ok


Copy this code and paste it in your HTML
  1. function validarFecha( $fecha, $invertir = 1 ){
  2. if( $invertir == 1 ) $fecha = implode( "-", array_reverse( preg_split( "/\D/", $fecha ) ) );
  3. $fecha = explode("-",$fecha);
  4. if( sizeof( $fecha ) != 3 ) return false;
  5. if( checkdate( $fecha[1],$fecha[0],$fecha[2] ) ) return true;
  6. else return false;
  7. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.