go top

MEGA Sale

  • Colection of 65 PHP scripts for $4.29 each

Get 65 PHP scripts in a bundle for $4.29 each!

View Offer

Generate consecutive invoice numbers using PHP

Monday, 26th May, 2014  /  PHP Tutorials  /  5 Comments
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.

Share on:

5 Comments to "Generate consecutive invoice numbers using PHP"

Marvic

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

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

    Please, be polite and helpful and do not spam or offend others! We promise you will be treated the same way!

    Log in to your account to post your comments. If you still haven't joined our community yet, you can create your FREE account now!

    Posting tip:
    If you use code in your comments, please put it in these tags [php], [sql], [css], [js] PHP code example: [php] echo date("Y-m-d"); [/php]

    Thank you,
    PHPJabbers Team

    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