How to compare two date and validate fields

Asked

Viewed 48 times

0

I need to compare two date fields in the same form, and the difference between them has to be over 18 years, otherwise an error must be issued.

<form>

    <input type="date" name="datanascimento">
    <input type="date" name="dataIngresso">

    <input type="submit" value="Enviar">

</form>
  • Share your code with us, otherwise we won’t be able to help.

2 answers

1

Good night, use the function diff of PHP.

    $data1 = new DateTime( '2012-11-11' );
    $data2 = new DateTime( '1994-01-01' );

    $intervalo = $data1->diff( $data2 );

    echo "Intervalo é de {$intervalo->y} anos, {$intervalo->m} meses e {$intervalo->d} dias";

0

Gives an ID to each of the dates, opens a script tag, for JS, calls the ID’s and makes the calculation with them, uses Document.getElementById to catch the ids.

Browser other questions tagged

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