Jump to content

Recommended Posts

Greetings to mjdamato! This is the code I've been looking. However, I want my Date of Birth to like the Facebook or Tagged with the additional "Month:" "Day:" and "Year:" in the selection option. Sad to say this creates a bug on the code. The number 28 is missing in the Day option.

This is my code for the Birth Date form :

 

<?php

$monthOptions = '<option value="0" id="month_option">Month:</option>';

$dayOptions = '<option value="0" id="day_option">Day:</option>';

$yearOptions = '<option value="0" id="year_option">Year:</option>';

$nowYear = date("Y");

for($month=1; $month<=12; $month++){

    $monthName = date("F", mktime(0, 0, 0, $month)); // F  Full Month name

    $monthOptions .= "<option value=\"{$month}\">{$monthName}</option>\n";

}

 

for($day=1; $day<=31; $day++){

    $dayOptions .= "<option value=\"{$day}\">{$day}</option>\n";

}

 

for($year=1930; $year<=$nowYear; $year++){

    $yearOptions .= "<option value=\"{$year}\">{$year}</option>\n";

}

?>

 

<select name="month" id="month" onchange="updateDays();">

<?php echo $monthOptions; ?>

</select>

 

<select name="day" id="day">

<?php echo $dayOptions; ?>

</select>

 

<select name="year" id="year" onchange="updateDays();">

<?php echo $yearOptions; ?>

</select>

 

 

post-130695-13482403155938_thumb.jpg

Link to comment
https://forums.phpfreaks.com/topic/254346-date-of-birth-form-adjustment/
Share on other sites

Just to elaborate, this topic is a continuation of http://www.phpfreaks.com/forums/index.php?topic=290078.0

 

The complete code is this

 

<?php
$monthOptions = '<option value="0" id="month_option">Month:</option>';
$dayOptions = '<option value="0" id="day_option">Day:</option>';
$yearOptions = '<option value="0" id="year_option">Year:</option>';
$nowYear = date("Y");
for($month=1; $month<=12; $month++){
    $monthName = date("F", mktime(0, 0, 0, $month)); // F  Full Month name
    $monthOptions .= "<option value=\"{$month}\">{$monthName}</option>\n";
}

for($day=1; $day<=31; $day++){
    $dayOptions .= "<option value=\"{$day}\">{$day}</option>\n";
}

for($year=1930; $year<=$nowYear; $year++){
    $yearOptions .= "<option value=\"{$year}\">{$year}</option>\n";
}
?>
<html>
<head>
<script type="text/javascript">

function updateDays()
{
    //Create variables needed
    var monthSel = document.getElementById('month');
    var daySel   = document.getElementById('day');
    var yearSel  = document.getElementById('year');
    var monthVal = monthSel.value;
    var yearVal  = yearSel.value;
    
    //Determine the number of days in the month/year
    var daysInMonth = 31;
    if (monthVal==2)
    {
        daysInMonth = (yearVal%4==0 && (yearVal%100!=0 || yearVal%400==0)) ? 29 : 28;
    }
    else if (monthVal==4 || monthVal==6 || monthVal==9 || monthVal==11)
    {
        daysInMonth = 30;
    }
    
    //Add/remove options from days select list as needed
    if(daySel.options.length > daysInMonth)
    {   //Remove excess days, if needed
        daySel.options.length = daysInMonth;
    }
    while (daySel.options.length != daysInMonth)
    {   //Add additional days, if needed
        daySel.options[daySel.length] = new Option(daySel.length+1, daySel.length+1, false);
    }
    
    return;
}

</script>
</head>
<body>
Birthdate:<br />
<select name="month" id="month" onchange="updateDays();">
<?php echo $monthOptions; ?>
</select>

<select name="day" id="day">
<?php echo $dayOptions; ?>
</select>

<select name="year" id="year" onchange="updateDays();">
<?php echo $yearOptions; ?>
</select>

</body>
</html>

 

As you can see, upon testing on Feb 1932 and Feb 1996 (both leap year), the number 28 is missing.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.