How to pad single-digit numbers with a leading 0

4.3k views Asked by At

I am looping an array of one-digit and two-digit numbers.

When printing these values, I need to ensure that all values are shown as two-digit numbers.

I need a solution to prepend zeros to the single-digit numbers but leave the two-digit numbers unchanged.

In other words, I want to "left pad" a numeric string to a minimum of two digits by adding zeros.

How can I change my code to render leading 0's for values 1 through 9?

<?php foreach (range(1, 12) as $month): ?>
    <option value="<?=$month?>"><?=$month?></option>
<?php endforeach ?>

Expected result:

<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
7

There are 7 answers

0
jheddings On BEST ANSWER
<?php foreach (range(1, 12) as $month): ?>
  <option value="<?= sprintf("%02d", $month) ?>"><?= sprintf("%02d", $month) ?></option>
<?php endforeach?>

You'd probably want to save the value of sprintf to a variable to avoid calling it multiple times.

0
mickmackusa On

I don't like bouncing in and out of PHP tags when building my HTML markup. By including the option tags inside printf(), you never need to leave PHP.

foreach (range(1, 12) as $month) {
    printf('<option>%02d</option>', $month);
}

Furthermore, there is no reason to declare the value attribute in your option tags because it is identical to the option's text value. Even without the value attribute, JavaScript will still be able to grab the value of the tag from the text.

Only declare the option's value attribute if is different from the option text.

Therefore, it would be appropriate to declare the value attribute if you left it as an un-padded integer like this:

foreach (range(1, 12) as $month) {
    printf('<option value="%d">%02d</option>', $month, $month);
}

More elegantly, when using the same variable more than once, use an explicit "argnum" (argument number) followed by a literal dollar sign so that the variable is only passed into printf() once.

Single-quoted string (rendered as one line of HTML): (Demo)

printf('<option value="%1$d">%1$02d</option>', $month);

Double-quoted string (notice escaped $ before d): (Demo)

printf("<option value=\"%1\$d\">%1$02d</option>\n", $month);

A Nowdoc string: (Demo)

$format = <<<'HTML'
<option value="%1$d">%1$02d</option>

HTML;

foreach (range(1, 12) as $month) {
    printf($format, $month);
}
1
lemon On
$month = 1;
echo sprintf("%02d", $month);
out: 01

Use sprintf

1
cletus On

Use either str_pad():

echo str_pad($month, 2, '0', STR_PAD_LEFT);

or sprintf():

echo sprintf('%02d', $month);
0
Amit On

if($month < 10) echo '0' . $month;

or

if($month < 10) $month = '0' . $month;

1
WebTigers On

Here is a one-liner that does the job nicely without using foreach() expressly.

$options = array_map( function( $hour ) {
    return str_pad( $hour, 2, '0', STR_PAD_LEFT );
}, array_combine( range(1,12), range(1,12) ) );

With the result of:

Array
(
    [1] => 01
    [2] => 02
    [3] => 03
    [4] => 04
    [5] => 05
    [6] => 06
    [7] => 07
    [8] => 08
    [9] => 09
    [10] => 10
    [11] => 11
    [12] => 12
)
0
Lucas Bustamante On
array_map( function( $day ) {
    return str_pad( $day, 2, '0', STR_PAD_LEFT );
}, range(1, 31) );

Result:

Array
(
    [0] => 01
    [1] => 02
    [2] => 03
    [3] => 04
    [4] => 05
    [5] => 06
    [6] => 07
    [7] => 08
    [8] => 09
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 13
    [13] => 14
    [14] => 15
    [15] => 16
    [16] => 17
    [17] => 18
    [18] => 19
    [19] => 20
    [20] => 21
    [21] => 22
    [22] => 23
    [23] => 24
    [24] => 25
    [25] => 26
    [26] => 27
    [27] => 28
    [28] => 29
    [29] => 30
    [30] => 31
)

Works in PHP 5.3+. Try it yourself: https://3v4l.org/aE6HO