- Colection of 65 PHP scripts for $4.29 each
Generate consecutive invoice numbers using PHP
<?php
for ($number = 1; $number<10; $number++) {
echo $number;
}
?>
The above code will generate the following numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. You will have to manually add the extra zeros in front of each number. However, there is a very useful PHP function str_pad which will add any string (in our case this is just 0) to the start or end of a string so it becomes a certain length.
We've created a simple function where you can specify start number, count and how many digits the generated numbers should be.
generate_numbers($start, $count, $digits)
$start - is the number for your first invoice
$count - how many invoice numbers you want to generate
$digits - how many digits the generated numbers should be
<?php
function generate_numbers($start, $count, $digits) {
$result = array();
for ($n = $start; $n < $start + $count; $n++) {
$result[] = str_pad($n, $digits, "0", STR_PAD_LEFT);
}
return $result;
}
?>
So if you call the function like this
$numbers = generate_numbers(9992, 20, 10);
it will generate an array $numbers with the following values
Array
(
[0] => 0000009992
[1] => 0000009993
[2] => 0000009994
[3] => 0000009995
[4] => 0000009996
[5] => 0000009997
[6] => 0000009998
[7] => 0000009999
[8] => 0000010000
[9] => 0000010001
[10] => 0000010002
[11] => 0000010003
[12] => 0000010004
[13] => 0000010005
[14] => 0000010006
[15] => 0000010007
[16] => 0000010008
[17] => 0000010009
[18] => 0000010010
[19] => 0000010011
)
As you can see the first eight numbers are 4 digits long (9992 to 9999) and hence we have six 0 added in front of them. Then we have 5 digits long numbers (10000 to 10011) and for these numbers we have five 0 added in front of them.
5 Comments to "Generate consecutive invoice numbers using PHP"

Marvic / March 7, 2020 at 18:02 pm
Hi y try to use your example and I have this error, but is the same code
Fatal error: Using $this when not in object context in C:xampphtdocsclinicaticket.php on line 22
$start;
$count;
$digits;
function generate_numbers($start, $count, $digits) {
$result = array();
for ($n = $start; $n < $start + $count; $n++) {
$result[] = str_pad($n, $digits, "0", STR_PAD_LEFT);
}
return $result;
}
$numbers = generate_numbers(1, 500, 5);
$order_no = array(
'order_no' => $numbers[0],
);$order_no++;
$this->session->set_userdata($order_no);
?>

Nic / January 21, 2017 at 05:18 am
I am trying to create a simple system that dose the following
1) generates job numbers that have the year followed by up to a 6 digit number
eg. #2017.123456 (would like it to update the year as required, based on the Australian financial year)
2) works in windows 7/8/10
3) can be packaged into a simple gui with a 1 click option to generate the number.
4) never stops. must continue to work year after year.
Ideas?

Shpat / February 19, 2016 at 17:36 pm
Hi,
How can we increment this number after the session ends;
example :
function generate_numbers($start, $count, $digits) {
$result = array();
for ($n = $start; $n < $start + $count; $n++) {
$result[] = str_pad($n, $digits, "0", STR_PAD_LEFT);
}
return $result;
}
$numbers = generate_numbers(1, 1, 4);
$order_no = array(
'order_no' => $numbers[0],
);$order_no++;
$this->session->set_userdata($order_no);
}