Page 1 of 1

Date format from form help.

Posted: Wed Aug 15, 2007 12:28 pm
by lafflin
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello, I am trying to use an array to create a drop down menu populaterd with month names. The Keys to my array are 0-12. When the HTML is created for this the keys in my array become the values in my drop down menu. This gives me the values of 0-12 (zero being the default which will in turn create an error). The problem is that to insert a date into my DB I need a month value with two digits. I tried to create the array as such:

Code: Select all

// Make the months array.
$months = array (00=>"",'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August','September', 'October', 'November', 'December');
however the following is still produced:

Code: Select all

 <select name="month">
<option value="0"></option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
The problem with this is that all the month values need to be in a two digit format for insertion into my DB.

I'm sure anyone who actually knows PHP knows the solution to this, I'm just learning. I have checked the manual thinking that number_format would be where the solution lies, but appearantly it's not.

Any help given here is much appreciated.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Aug 15, 2007 12:37 pm
by Zoxive
You can probably change the function you are using to create the drop down to add an extra 0 at the beginning of the key values.

Code: Select all

array(
  00=>'0',
  01=>'1'
);
/* Becomes
array (
  0 => '0',
  1 => '1',
)
*/
Since that is happening your best bet is to add it to the function you are using to add a 0.

Re: Date format from form help.

Posted: Wed Aug 15, 2007 12:47 pm
by Christopher
Make your array keys strings:

Code: Select all

// Make the months array.
$months = array (
    '00'=>"",
    '01'=>'January',
    '02'=>'February', 
    '03'=>'March', 
    '04'=>'April', 
    '05'=>'May', 
    '06'=>'June', 
    '07'=>'July', 
    '08'=>'August',
    '09'=>'September', 
    '10'=>'October', 
    '11'=>'November', 
    '12'=>'December',
    );

Posted: Wed Aug 15, 2007 12:49 pm
by miro_igov

Code: Select all

<option value="<?php if(strlen($i)==1) echo '0'.$i; else echo $i; ?>">January</option>

Date format from form help needed [solved]

Posted: Wed Aug 15, 2007 1:10 pm
by lafflin
Thank you all for helping me, the greenest of noobs, out.
Arb, consider yourself the man.