Web Design Blog

This is where we store some of our Web Development thoughts, tips and tricks, just because we like to share.

PHP – Populate Date Dropdowns – HTML Forms

Ok, i’m going to quickly jot down some PHP code, which dynamically populates date fields (dd/mm/yyyy) for HTML forms. I’m always using this code, but I never write it down, so i’m forever retyping it!

Dynamic Date Dropdowns

To generate the date dropdown fields shown above, you could hardcode the values, like this….

1
2
3
4
5
6
7
8
9
10
<select name="year">
<option value="1999">1999</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
</select>

But that’s a very tedious and long way of doing it. A better solution is to use a for loop in PHP to generate the dates. The following code i’m using automatically generates the dates with a few lines of PHP…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<select name="day">
	<?for($day=1; $day <= 31; ++$day):?>
		<?
                //this code is adding a 0 before all values less than 10
                //you don't need this code, but I prefer to have 2 digit values
		if($day < 10)
		{
			$day = "0".$day;
		}
		?>
		<option value="<?=$day?>"><?=$day?></option>
	<?endfor;?>
</select>
<select name="month">
	<?for($month=1; $month <= 12; ++$month):?>
		<?
                //this code is adding a 0 before all values less than 10
                //you don't need this code, but I prefer to have 2 digit values
		if($month < 10)
		{
			$month = "0".$month;
		}
		?>
		<option value="<?=$month?>"><?=$month?></option>
	<?endfor;?>
</select>
<select name="year">
	<?
        //The 60 value here is the amount of years to go back
	$year = date("Y") - 60;
        //The 60 value here is the amount of years to go forward
        //because i went back 60 years, i'm going forward 60 years so the dropdown will always have the current year, going back 60 years.
	for ($i = 0; $i <= 60; ++$i)
	{
		echo "<option>$year</option>"; ++$year;
	}
	?>
</select>

The above code will generate dropdowns for day, month and year! I’ve commented in areas where I felt appropriate.

Does anyone know a better way to do this with PHP? Would love to hear your tips…

10 Jun 2010 / 1 Comment / PHP Scripts, Tips & Tricks / by Maruf

1 Comment

  1. Christian Zülli
    01/05/2011
    1

    Hi,
    very useful code for populating forms with the date fields.
    It has one big disadvantage to prevent it from use professionally:
    The number of days for each month are set to 31. In real life they differ in each month. (January = 31, February = 28 or 29, March = 31, April = 30 days etc.)

    Would you like to fix that? of course a much more extensive coding needed.

    I personally handle this with Javascript, but also that has it’s negative side if someone has Javascript turned off.

    Have a good day,
    Chris

Leave a Reply

© 2012 BrightCherry :)