Question

I have written the following code in my HTML form:

<select name="createdDay"><?php for ($i=1; $i <= 31 ; $i++) 
{echo '<option>' . $i . '</option>';} ?>

The index only goes up in single digits to 10 obviously, is there a way that I can use two digits please? Example 01, 02, 03, 04......

I have tried pre-fixing a 0 or have i=01 but to no avail!

Was it helpful?

Solution

Use sprintf(), which allows you to specify formatting/padding chars, like

$formatted = sprintf('%02d', $i); // 1 -> 01, 10 -> 10
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top