d
Amit DhamuSoftware Engineer
 

Leading Zeroes

1 minute read 00000 views
function leadingZeros($num, $numDigits) {
   return sprintf('%0' . $numDigits . 'd', $num);
}

foreach(range('01', '12') as $month) {
    echo leadingZeros($month, 2);
}

/* Foreach loop prints out:
    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
*/