4

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
  • 960
  • 9
  • 19
Jacques
  • 107
  • 1
  • 9
  • 3
    Possible duplicate of [Zero-pad digits in string](https://stackoverflow.com/questions/324358/zero-pad-digits-in-string) – Shira 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
  • 3,012
  • 1
  • 20
  • 18
B. Desai
  • 16,414
  • 5
  • 26
  • 47
2

sprintf has this build in:

$number = sprintf("%07d", $input)
colburton
  • 4,685
  • 2
  • 26
  • 39