Page 1 of 1

My Date Picker Problem

Posted: Wed Nov 02, 2005 5:33 am
by mhouldridge
Hi,

I am trying to create a selection box with date values in, for instance;

2 november 2005
3 november 2005
4 november 2005 ....and so on

Here is my code;

Code: Select all

$Year = date("Y");  
echo "<SELECT NAME=Date><OPTION>Choose One</OPTION>n";
$Day = 1;  
	while ($Day <= 31) {  
     $Day++;  
$EndYear = $Year + 2;  
	while ($Year <= $EndYear)
	{ 
	$Year++; 
	echo "<OPTION VALUE=$Day, January, $Year>$Day January, $Year</OPTION>\n";  
	echo "<OPTION VALUE=$Day, February, $Year>$Day February, $Year</OPTION>\n";  
	echo "<OPTION VALUE=$Day, March, $Year>$Day March, $Year</OPTION>\n";  
	echo "<OPTION VALUE=$Day, April $Year>$Day April, $Year</OPTION>\n";  
	echo "<OPTION VALUE=$Day, May, $Year>$Day May, $Year</OPTION>\n";  
	echo "<OPTION VALUE=$Day, June, $Year>$Day June, $Year</OPTION>\n";  
	echo "<OPTION VALUE=$Day, July, $Year>$Day July, $Year</OPTION>\n";  
	echo "<OPTION VALUE=$Day, August, $Year>$Day August, $Year</OPTION>\n";  
	echo "<OPTION VALUE=$Day, September, $Year>$Day September, $Year</OPTION>\n";  
	echo "<OPTION VALUE=$Day, October, $Year>$Day October, $Year</OPTION>\n";  
	echo "<OPTION VALUE=$Day, November, $Year>$Day November, $Year</OPTION>\n";  
	echo "<OPTION VALUE=$Day, December, $Year>$Day December, $Year</OPTION>\n";  
	echo "</SELECT>\n";
	 
    }
   }

This thing is not working, just displays lots of dates running right through to year 2097

Please help.

Posted: Wed Nov 02, 2005 6:18 am
by foobar
Your code is dismally screwd up.

Code: Select all

$leap = array(1, 0, 0, 0, 1, 0); //<- Not accurate! Look up the leapyears.
$years = array(2000, 2001, 2002, 2003, 2004, 2005, 2006); //<- List of years

echo "<SELECT NAME=Date><OPTION>Choose One</OPTION>n"; 

foreach ($years as $year) {  //Cycle thru years

  for ($i = 1; $i<=12; $i++) { //Cycle thru months

    $day = 1; //Start at day 1

     while ($day <= date('t', $month = mktime(0, 0, 0,  $i, 1, $year)))  { //Cycle thru days
       echo "<OPTION VALUE={$day}, "
                  .date('m', $month)
                  .", {$year}>{$day} "
                  .date('m', $month).", {$year}</OPTION>\n"; 
       $day++;
    }
  }
}

echo "</SELECT>\n";
Note: This was not tested, but coded "on-the-fly". ;)

Cheers,

Tom

Edit: In your option values, I would recommend using something that's easier for php to understand. You can generate a timestamp via strtotime(), for instance.

Posted: Wed Nov 02, 2005 6:22 am
by Jenk
You need to encapsulate values in quotes

Code: Select all

<option value="blah">blah</option>
you can also use

Code: Select all

date('L', $timestamp);
to see if the year is a leap year. (1 for yes, 0 for no)

EDIT: and as a suggesting for what value to assign in the option, just use the unix timestamp from mktime().

Posted: Wed Nov 02, 2005 7:33 am
by mhouldridge
Yep, Thats sorted it now.

Thanks guys!