Jump to content

Recommended Posts

I used some PHP code to build the select lists to cut down on the HTML code needed. But, the javascript function below will ensure the "day" select list has the correct number of days according to the month/year the user has selected:

 

<?php
$monthOptions = '';
$dayOptions = '';
$yearOptions = '';

for($month=1; $month<=12; $month++)
{
    $monthName = date("M", mktime(0, 0, 0, $month));
    $monthOptions .= "<option value=\"{$month}\">{$monthName}</option>\n";
}
for($day=1; $day<=31; $day++)
{
    $dayOptions .= "<option value=\"{$day}\">{$day}</option>\n";
}
for($year=1900; $year<=2010; $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>

Link to comment
https://forums.phpfreaks.com/topic/194184-date-of-birth-form/#findComment-1021718
Share on other sites

  • 1 year later...

This script works great in a form. What am I dont wrong for this form mail?

<?php
// Receiving variables
@$month = addslashes($_POST['month']);
@$day = addslashes($_POST['day']);
@$year = addslashes($_POST['year']);

//Sending Email to form owner
$pfw_header = "From: VBS\n"
  . "Reply-To: VBS\n";
$pfw_subject = "VBS";
$pfw_email_to = "your@email.com";
$pfw_message = "Birthday: $month $day $year\n";
@mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;

echo("<p align='center'><font face='Arial' size='3' color='#FF0000'>fixed</font></p>");
?> 

Link to comment
https://forums.phpfreaks.com/topic/194184-date-of-birth-form/#findComment-1226512
Share on other sites

(yearVal%4==0 && !(yearVal%100%4))?29:28

 

sry :$ I just saw some long ass inline-if so I had to shorten it, ocd D:

 

Too bad your shortened code produces incorrect results. Using your formula the year 2100 would be a leap year, but it is not. Basically your code will be wrong once every century (on the "100" year marks) 3 out every 4 centuries: 1700, 1800, 1900, (correct on 2000) 2100, 2200, 2300, etc.

Link to comment
https://forums.phpfreaks.com/topic/194184-date-of-birth-form/#findComment-1226956
Share on other sites

ahhhh I seeee, I was wondering why the modulo 400 was in there, I was like, theres 100, 400, 4, they'd all work just off 4 if it was that simple :D

 

but the gregorian calendar needs updating pretty badly, coz as it stands every 4 years we're actually GAINING like 45 minutes or so..

 

anyways, I also wikipedia'd it, and this is what I've come up with, and so I've written my interpretation..

if year modulo 400 is 0

      then is_leap_year

else if year modulo 100 is 0

      then not_leap_year

else if year modulo 4 is 0

      then is_leap_year

else

      then not_leap_year

((!(2012%400) || !(2012%4)) && (2012%100)?29:28)

which seems a little different from what you've written, so I ask, what am I not seeing here? :o

Link to comment
https://forums.phpfreaks.com/topic/194184-date-of-birth-form/#findComment-1227021
Share on other sites

((!(2012%400) || !(2012%4)) && (2012%100)?29:28)

which seems a little different from what you've written, so I ask, what am I not seeing here? :o

 

There was nothing wrong with my original solution. But, oh well, your proposed logic would work as well, except you implemented it wrong. You parenthesis are not correct. The above code says if (year is divisible by 400 OR 4) AND NOT divisible by 100) then it is a leap year. The AND clause negates the 400 condition (a year cannot be divisible by 400 and not be divisible by 100).  You want something like this:

 

IF ( (Year div by 400) OR ( (Year div by 4) AND (Year NOT Div by 100) ) = leap year

((!(yearVal%400) || (!(yearVal%4) && (yearVal%100))) ? 29 : 28);

 

The logic I used was:

If ( (Year div by 4) AND ( (Year NOT Div by 100) OR (Year Div by 400) ) = Leap year

 

Which, if you compare my original code with the code I just corrected for your logic, are both exactly the same number of characters. I just think my solution is easier to read. Besides, using something like  !(yearVal%4) to test if the year is divisible by 4 seems counter intuitive since you are using a NOT to return a true condition.

Link to comment
https://forums.phpfreaks.com/topic/194184-date-of-birth-form/#findComment-1227046
Share on other sites

((!(2012%400) || !(2012%4)) && (2012%100)?29:28)

which seems a little different from what you've written, so I ask, what am I not seeing here? :o

 

There was nothing wrong with my original solution. But, oh well, your proposed logic would work as well, except you implemented it wrong. You parenthesis are not correct. The above code says if (year is divisible by 400 OR 4) AND NOT divisible by 100) then it is a leap year. The AND clause negates the 400 condition (a year cannot be divisible by 400 and not be divisible by 100).  You want something like this:

 

IF ( (Year div by 400) OR ( (Year div by 4) AND (Year NOT Div by 100) ) = leap year

((!(yearVal%400) || (!(yearVal%4) && (yearVal%100))) ? 29 : 28);

 

The logic I used was:

If ( (Year div by 4) AND ( (Year NOT Div by 100) OR (Year Div by 400) ) = Leap year

 

Which, if you compare my original code with the code I just corrected for your logic, are both exactly the same number of characters. I just think my solution is easier to read. Besides, using something like  !(yearVal%4) to test if the year is divisible by 4 seems counter intuitive since you are using a NOT to return a true condition.

 

according to wikipedia's calculation pseudocode my script will not trigger the if statement if it is NOT A LEAP YEAR, and if it is divisible 100 after divisible by 400 fails..

 

else if year modulo 100 is 0

      then not_leap_year

 

if you check out my proposed code in the console, it works perfectly, I was just noting that the pseudocode proposed by wikipedia doesn't match to yours exactly, and simply asking what the difference is lol

Link to comment
https://forums.phpfreaks.com/topic/194184-date-of-birth-form/#findComment-1227237
Share on other sites

according to wikipedia's calculation pseudocode my script will not trigger the if statement if it is NOT A LEAP YEAR, and if it is divisible 100 after divisible by 400 fails..

 

else if year modulo 100 is 0

      then not_leap_year

 

if you check out my proposed code in the console, it works perfectly, I was just noting that the pseudocode proposed by wikipedia doesn't match to yours exactly, and simply asking what the difference is lol

I'm not following your first statement. But, your proposed code is wrong. Sure, it works for 2012, but that doesn't mean it is is valid. It fails to correctly identify the year 2000 as a leap year (as it will for 2400, 2800, etc.)

 

As for the pseudo code on the Wiki, it is valid. As is my alternative approach

Link to comment
https://forums.phpfreaks.com/topic/194184-date-of-birth-form/#findComment-1227253
Share on other sites

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.