Date of Birth – Form using PHP

Hi, this is a very basic tutorial . I must say its just a snippet of code which you can use to take an input of Date of birth in your form. I have made it using PHP. If you  are newbie this is very useful for you. Its simple to understand. Here is the code:

Firstly, these are the simple functions that can help you generate a date.These are loops and arrays that do all the trick for you.

<?php
//generated years from 1900 to 2010

    function yearOptions( $endYear = '2010', $startYear = '1900' )
    {
        for ( $i = $startYear; $i <= $endYear; $i++ )
        {
            ( $i == date('Y') ) ? $selected = ' selected="selected"'
   : $selected = '';
            echo '<option value="'.$i.'"'.$selected.'>'.$i.
   '</option>'."\n";
        }
    }

//generates teh list of all the months   
    function monthOptions()
    {
        $months = array( 1 => "January", "February", "March",
  "April", "May", "June", "July", "August",
  "September", "October", "November", "December" );

        foreach ( $months as $monthNo => $month )
        {
         ( $monthNo == date('n') ) ? $selected = ' selected=
   "selected"' : $selected = '';
          echo '<option value="'.$monthNo.'"'.$selected.'>'.$monthNo.
    ' - '.$month.'</option>'."\n";
        }
    }

 //generates the list of 31 days  
    function dayOptions()
    {
        for ( $i = 1; $i <= 31; $i++ )
        {
            ( $i == date('j') ) ? $selected = ' selected="selected"'
   : $selected = '';
            echo '<option value="'.$i.'"'.$selected.'>'.$i.
   '</option>'."\n";
        }
    }
?>

Secondly, this is the html+PHP to make the front end, that means to put the form on a web page. here we call the functions created above.

<select>
<?php dayOptions(); ?>
</select>
-
<select>
<?php monthOptions(); ?>
</select>
-
<select>
<?php yearOptions(); ?>
</select>
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s