开发者

Add characters to month loop?

I currently have a php loop running exactly how I need it with proper validations (in both php and javascript) with one exception, if 开发者_JS百科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>

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


Try this when outputting the month:

sprintf("%02d", $month); // 01, 02 .. 09, 10, 11...


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜