search

Home  >  Q&A  >  body text

Convert 1 digit number to HH:mm:ss format in PHP

<p>In PHP, how do I convert a 1-digit number to a full time representation, for example: </p> <pre class="brush:php;toolbar:false;">0 1 2 ... 17 18 19</pre> <p>Convert to a complete time representation, for example: </p> <pre class="brush:php;toolbar:false;">00:00:00 01:00:00 02:00:00 ... 17:00:00 18:00:00 19:00:00</pre> <p>I tried using the strtotime() and date() functions but didn't really grasp their usage. </p>
P粉512729862P粉512729862596 days ago624

reply all(1)I'll reply

  • P粉969253139

    P粉9692531392023-08-07 10:31:14

    Use sprintf to zero-fill to two digits:

    foreach (range(0, 23) as $i) {
       echo sprintf('%02d:00:00' . PHP_EOL, $i);
    }

    reply
    0
  • Cancelreply