Grow your CSS skills. Land your dream job.

Last updated on:

Force Leading Zero

<?php
  function forceLeadingZero($int) {
    return (int)sprintf('%02d',$int);
  }
?>

Forces leading zero to integers.

was | now

1 | 01
2 | 02
3 | 03

10 | 10
100 | 100
99 | 99

Reference URL

Comments

  1. LM
    Permalink to comment#

    str_pad?

  2. LM
    Permalink to comment#

    str_pad($input, 2, “0″, STR_PAD_LEFT);

  3. str_pad is probably more semantically accurate, but sprintf() is faster.

Leave a Comment

Current day month ye@r *

*May or may not contain any actual "CSS" or "Tricks".