views:

31

answers:

2

I currently have a php loop running exactly how I need it with proper validations (in both php and javascript) with one exception, if the month is less than 2 digits, (i.e. 1,2,3,4), I need for a '0' to appear before:

01 - January 02 - February ... 10 - October

My code for the loop is currently:

<select name="Month">
                <option value="">Month</option> 
                    <?php
                    for ($i=1; $i<=12; $i++)
                    {
                    echo "<option value='$i'";
                    if ($fields["Month"] == $i)
                    echo " selected";
                    echo ">$i</option>";
                    }
                    ?>          
</select>

any ideas?

Also note, this month date is being stored in session, not interested in printing to screen

+7  A: 

Try this when outputting the month:

sprintf("%02d", $month); // 01, 02 .. 09, 10, 11...
Mike B
@Mike - not sure I follow, I'm not looking to output this information. I'm storing in Session
JM4
+1  A: 

Use sprintf($format, [$var, [$var...).

Here, have some code:

function padLeft($char, $s, $n) {
    return sprintf("%" . $char . $n . "d", $s);
}
function padWithZeros($s, $max_length) {
    return padLeft('0', $s, $max_length);
}
Christian Mann