-1

Seems like this should be simple but I can't get it to work.

I have credit card expiration months in format n (without leading zeros) and need it to be m (with leading zeros)

How do I convert an n month to an m?

James
  • 161
  • 1
  • 10

3 Answers3

1

You can use sprintf to automatically pad a value to 2 (or more) characters with leading zeros:

echo sprintf("%02d", 9);

Output

09
Nick
  • 138,499
  • 22
  • 57
  • 95
1
function checkTime(i) {
    if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10 because I'm fancy
    return i;
}
Nick
  • 138,499
  • 22
  • 57
  • 95
Dice
  • 236
  • 1
  • 8
0

you can use sprintf function for this...

<?php
 $monthnumber = date('m');
 $month = sprintf('%02d', $monthnumber);
 echo $month;
?>