lopes_andre Posted March 4, 2010 Share Posted March 4, 2010 Hi, I need to develop a form with the date of birth field(dropdown box). There is something developed that can help with the problem of the days of the month? Days 30, 31, and 28 (or 29) when specific months of the year are selected? Best Regards, Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 4, 2010 Share Posted March 4, 2010 Unless you want the user to submit the form after selecting the month (or use AJAX) - neither of which makes sense - then you should be using JavaScript for this. Moving post to correct forum. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 4, 2010 Share Posted March 4, 2010 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> Quote Link to comment Share on other sites More sharing options...
MikeyKs1 Posted June 7, 2011 Share Posted June 7, 2011 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>"); ?> Quote Link to comment Share on other sites More sharing options...
Maq Posted June 7, 2011 Share Posted June 7, 2011 @MikeyKs1, if you have a new question please start your own thread. This one is over a year old. Also use code tags around your code. Quote Link to comment Share on other sites More sharing options...
RussellReal Posted June 8, 2011 Share Posted June 8, 2011 (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: Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 8, 2011 Share Posted June 8, 2011 (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. Quote Link to comment Share on other sites More sharing options...
RussellReal Posted June 8, 2011 Share Posted June 8, 2011 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 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? Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 8, 2011 Share Posted June 8, 2011 ((!(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? 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. Quote Link to comment Share on other sites More sharing options...
RussellReal Posted June 9, 2011 Share Posted June 9, 2011 ((!(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? 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 9, 2011 Share Posted June 9, 2011 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 Quote Link to comment Share on other sites More sharing options...
RussellReal Posted June 9, 2011 Share Posted June 9, 2011 Ahhh you're right! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.