Hi all,

i'm building a simple date input with php using drop down list box
the problem i faced is "leap year"

how do i display 29 day in the drop down list box while user selected a leap year
if user make an option on day first then only go to
Select the year what i'm suppose to do ?

here my code:

<tr><td width="20%" bgcolor="#DDE8EE"><span class="input_name">Effective Date :</span></td><td bgcolor="#DDE8EE">
		<select name="Year" id="Year">
		<option value="">Year</option>
		<?php
			$end = (date("Y")+3);
			for($y=2010;$y<$end;$y++)
			{
				echo "<option value=".$y.">".$y."</option>";
			}
		?>
		</select>
		<select name="Month" id="Month">
		<option value="">Month</option>
		<?php
			$mon = array("","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec");
			for($m=1;$m<=12;$m++)
			{
				echo "<option value=".$m.">".$mon[$m]."</option>";
			}
		?>
		</select>
		<select name="Day" id="Day">
		<option value="">Day</option>
		<?php
			$day = array("",31,28,31,30,31,30,31,31,30,31,30,31);
			echo "<SCRIPT LANGUAGE="javascript"><!--n";
			echo "return LeapYearChecking();n";
			echo "// --></SCRIPT>n";
			LeapYearChecking()
			for($i=2010;$i<$end;$i++)
			{
				if($i%400==0||($i%100!=0 && $i%4==0))
				{$day[2] = 29;}
				else{$day[2] = 28;}
			}
		
		?>
		</select></td></tr>

If you got better advice for the date input in another format like date time picker
please kindly share the link here i'll be thankful T^T!

Do not restrict select element by user choice. You must try to to validate date after user submits the form using some javascript function.

<script lang='javascript'>

var daysOfMonth = new Array(
  31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
);

var daysOfMonthLY = new Array(
  31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
);
function isLeapYear(year) {
  year = year - 0;
  if ((year/4)   != Math.floor(year/4))   return false;
  if ((year/100) != Math.floor(year/100)) return true;
  if ((year/400) != Math.floor(year/400)) return false;
  return true;
}

function isValidDate(day, month, year) {
  day = day - 0; month = month - 0; year = year - 0;
  if ((isLeapYear(year) && day > daysOfMonthLY[month-1]) ||
     (!isLeapYear(year) && day > daysOfMonth[month-1]))
    return false;
  else
    return true;
}


function checkdate()
{


	if (!isValidDate(document.register.day.value, document.register.month.value, document.register.year.value))
	{
	 	alert('Invalid Date');
		document.register.day.focus();		 	
		return false;
	}
}
</script>

Thank you very much for helping me !

i got one more little problem which is Display day according to month
that selected by user
e.g. The drop down display only
28 on Feb
31 on Jan
30 on April
for user to select especially while leap year display 29 on Feb... @_@

seriously i can't brain this. Please Help.... T^T

Thank you * 100

Member Avatar for Zagga

Hi Nybuler,

As urtrivedi suggested, don't restrict what the user can enter. Let them enter 31st February if they wish.

You can use the PHP date functions to validate the input and correct errors (like the one above).
http://www.w3schools.com/php/php_ref_date.asp

Hope this helps.
Zagga

what I have suggested will be done at client side using javascript and without submitting a form. In my code case user may select invalid date but can not submit that invalid date.

if you want to use php then you must use ajax or you must submit form.

what I have suggested will be done at client side using javascript and without submitting a form. In my code case user may select invalid date but can not submit that invalid date.

if you want to use php then you must use ajax or you must submit form.

Thank you urtrivedi nad zagga! my boss accept the method already, so my problem is solved
Thank you very much :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.