3

I have an Auto-Increment field in a database.

I want these numbers to be prepended by zeros, to a maximum length of seven.

Example:

Original number: 1 Desired result: 0000001

or

Original number 768 Desired result 0000768

How would I achieve this in PHP?

Sunil Rajput
  • 930
  • 9
  • 19
Jacques
  • 97
  • 1
  • 9
  • 3
    Possible duplicate of [Zero-pad digits in string](https://stackoverflow.com/questions/324358/zero-pad-digits-in-string) – ShiraNai7 Jul 08 '17 at 12:51

2 Answers2

7

Use str_pad function of PHP

$input = 1;
$number = str_pad($input, 7, "0", STR_PAD_LEFT);
paaacman
  • 2,972
  • 1
  • 18
  • 18
B. Desai
  • 16,264
  • 5
  • 24
  • 44
2

sprintf has this build in:

$number = sprintf("%07d", $input)
colburton
  • 4,645
  • 2
  • 23
  • 37