go top

MEGA Sale

  • Colection of 65 PHP scripts for $6 each

Generate consecutive invoice numbers using PHP

Limited offer!

Get a huge collection of PHP scripts in a special bundle!

Mega Sale Price
Usually when it comes to invoicing you will need to generate consecutive numbers. For example in Bulgaria the invoice numbers should be 10 digits long. So if you start from your first invoice its number should be 0000000001, then next invoice should be 0000000002, and so on. As you can imagine you cannot just make a cycle to generate these numbers like

<?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.

Special Offer!

Get all 65 PHP scripts in a bundle for $6 each only!

Mega Sale Offer

4 Comments to "Generate consecutive invoice numbers using PHP"

  • Nic

    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

    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);



    }

  • troleen

    troleen

    September 18, 2015 at 16:59 pm

    this was helpful to me..thank you

  • NOno

    NOno

    March 8, 2015 at 16:17 pm

    Thanks for sharing, it is very helpful.
    Please advise how to generate invoice for each quantity purchased instead of invoice for total quantity.
    for example, there are 5 quantity purchased then there are 5 invoice generated .

    thanks,

Add your comment

Captcha
    • Free Scripts

      Add great new functionalities to your website with our Free Scripts collection.

      Free scripts
    • PHP Scripts

      Check our extensive collection of top-notch PHP Scripts that will enhance your website!

      Commercial PHP scripts