Validare data de nastere din formular si aflare varsta

Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
CristianAlexandru17
Mesaje:13

Validare data de nastere din formular si aflare varsta

Salut,
Ma tot chinui de 3 zile incoace sa validez manual o data (data nasterii).
Am incercat tot felul de metode dar nu-mi iese.
Codul meu arata in felu' urmator:

Cod: Selectaţi tot

<?php
    if($_POST['day'] == 7 && $_POST['month'] == 9 && $_POST['year'] == 1979){
         echo 'this';
   }
?>

<form action="" method="post" autocomplete="on">
               Day*: 
             <select name="day">
                   <option value="00">--Pick a day--</option>
                   <option value="01">01</option>
                   <option value="02">02</option>
                        <option value="03">03</option>                             
                        <option value="04">04</option>                             
                        <option value="05">05</option>                             
                        <option value="06">06</option>                             
                        <option value="07">07</option>                             
                        <option value="08">08</option>                             
                        <option value="09">09</option>                             
                        <option value="10">10</option>                             
                        <option value="11">11</option>                             
                        <option value="12">12</option>                             
                        <option value="13">13</option>                             
                        <option value="14">14</option>                             
                        <option value="15">15</option>                             
                        <option value="16">16</option>                             
                        <option value="17">17</option>                             
                        <option value="18">18</option>                             
                        <option value="19">19</option>                             
                        <option value="20">20</option>                             
                        <option value="21">21</option>                             
                        <option value="22">22</option>                             
                        <option value="23">23</option>                             
                        <option value="24">24</option>                             
                        <option value="25">25</option>                             
                        <option value="26">26</option>                             
                        <option value="27">27</option>                             
                        <option value="28">28</option>                             
                        <option value="29">29</option>                             
                        <option value="30">30</option>                             
                        <option value="31">31</option>        
                    </select>                         
               Month*: 
             <select name="month">
                  <option value="00">--Pick a month--</option>
                  <option value="01">January</option>
                  <option value="02">February</option>
                  <option value="03">March</option>
                  <option value="04">April</option>
                  <option value="05">May</option>
                  <option value="06">June</option>
                  <option value="07">July</option>
                  <option value="08">August</option>
                  <option value="09">September</option>
                 <option value="10">October</option>
                  <option value="11">November</option>
                  <option value="12">December</option>
             </select>
               Year*: 
             <select name="year">
                  <option value="0000">--Pick a year--</option>
                  <option value="1979">1979</option>
                <option value="1980">1980</option>
                <option value="1981">1981</option>
                <option value="1982">1982</option>
                <option value="1983">1983</option>
                <option value="1984">1984</option>
                <option value="1985">1985</option>
                <option value="1986">1986</option>
                <option value="1987">1987</option>
                <option value="1988">1988</option>
                <option value="1989">1989</option>
                <option value="1990">1990</option>
                <option value="1991">1991</option>
                <option value="1992">1992</option>
                <option value="1993">1993</option>
                <option value="1994">1994</option>
                <option value="1995">1995</option>
                <option value="1996">1996</option>
                <option value="1997">1997</option>
                <option value="1998">1998</option>
                <option value="1999">1999</option>
                <option value="2000">2000</option>
               </select>
       <input type="submit" value="Check">
</form>
Am luat ca exemplu data: 07 Septembrie 1979 mai sus in cod si problema e ca pentru orice zi, orice luna si orice an imi afiseaza mesajul setat de mine (de test) "this". De ce?
Am mai incercat asa.. sa zic ca vreau sa resping selectarea zilei 30 pentru luna februarie pt toti anii:

Cod: Selectaţi tot

<?php
    // ..
    if($_POST['year'] == 0000 && $_POST['month'] == 00 && $_POST['day'] == 00){
    $errors[] =  'Trebuie selectata o data de nastere!'; 
}
    if($_POST['day'] == 30 && $_POST['month'] == 02 && $_POST['year'] == 1979 || $_POST['year'] == 1980 || ... || $_POST['year'] == 2000 ){
         $errors[] = 'Acea zi nu exista in luna februarie a anului respectiv.';
}
   if($_POST['day'] == 29 && $_POST['month'] == 02 && $_POST['year'] == 1979 || $_POST['year'] == 1981 || $_POST['year'] == 1992 || $_POST['year'] == 1983... || $_POST['year'] == 1999){  //se exclud anii, ale caror luni februarie au 29 de zile
     $errors[] = 'Luna februarie a anului ales nu are 29 de zile, din pacate.';
}
   // ..
?>
Selectand alte zile, in afara de 30 imi da si aceasta eroare, chiar daca nu trebuie si nu inteleg de ce.
Ma puteti ajuta?

Si in plus, cum pot calcula varsta in functie de data nasterii a utilizatorului?

Multumesc,
Cristi

MarPlo Mesaje:4343
Salut
Am testat verificarea datei din formular, si arata "this" doar la acea data fixa, pentru altele nu arata. Nu stiu de ce la tine e cum spui.
Incearca sa vezi ce date sunt primite prin $_POST, cu:

Cod: Selectaţi tot

var_dump($_POST);
 
- Uite un cod php de calculare si afisare a varstei in ziua curenta:

Cod: Selectaţi tot

<?php
$bday = new DateTime('15.10.1976');
$today = new DateTime('00:00:00');

$diff = $today->diff($bday);   // object with the age

printf('%d years, %d month, %d days', $diff->y, $diff->m, $diff->d);
?>

CristianAlexandru17 Mesaje:13
Salut 'MarPlo',

Nu era necesar sa folosesc var_dump(), atata timp cat in PHP exista o functie foarte utila numita checkdate().

Multumesc pt scriptul pt calcularea varstei!!

Subiecte similare