Validate which input is date format

1

I have a form, with an input that is called Date Birth and is of date format, what I need is to validate the server side, using php that that input is a date.

for example, validate that the name is mandatory:

if (trim($nombre)==""){
    $errores['nombre'] = "¡El nombre es obligatorio!";
    $error = true;
    }

How would it be in that case to validate the date? thanks.

    
asked by David Bucci 13.12.2018 в 20:36
source

2 answers

2

Following a validation of your form, it would be:

$fecha_valida = strtotime($variable_fecha) ? true : false;
if (!$fecha_valida){
    $errores['fecha'] = "¡La fecha no es válida!";
    $error = true;
    }

I hope it serves you.

    
answered by 13.12.2018 / 20:42
source
1

You can use the function checkdate that you receive as parameter the month, day and year, assuming you get it by POST

$myDate = $_POST['fecha'];
$date_arr = explode('-', $myDate); //separamos la fecha que va separada por -
if (checkdate($date_arr[0], $date_arr[1], $date_arr[2])) {
        $errores['fecha'] = "Fecha  no válida";
         $error = true;
  }

taken from PHP Documentation

    
answered by 13.12.2018 в 20:44