date() Function - Using in drop down selections

I can’t seem to remember how to do this. I’ve been awhile away from using PHP.

I would like to set the default Selected to the current month. Is this the best way to do this, or is there a more streamlined way?

<?php $month=echo date(m); ?> >January >February

//etc for these too

    <option value="03">March</option>
    <option value="04">April</option>
    <option value="05">May</option>
    <option value="06">June</option>
    <option value="07">July</option>
    <option value="08">August</option>
    <option value="09">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
  </select>

Small suggestion. Instead of making the long list of create an array of the months names and then in a for loop cycle through.
Ex:

monthNum = date(n); // get the months number w/o leadings 0's laod month array // create the months names array for (i = 1; i <= 12 ; i++) { echo ' <option value="'. i .'" '; //start the option string (<option value="1" ) if (monthNum == i) { echo "selected"; // select it if applicable } echo '>' . month[i-1] . '</option>'; // close option string (>January</option>) }

Thank you for jogging my memory. I knew there was an easier, cleaner way to do this. Your tip is just what I was looking for. Thank you.

Sponsor our Newsletter | Privacy Policy | Terms of Service