Auto increment numbers in php

Hi guys,

I have simple problem,
Problem is that, i want that a php variable create date with numbers,

look like this:

$rtno = date(“md,here some numbers but auto increment start from 001
and going on”);
here md is month and date…
It will look like thsi (0107001).
Is i am right or to create it on some other format like
two variables like :
$autoincre= number(“generate numbers from 001”)

And call it in the $rtno variable,
How to do this any body have some idea…

Thanks in advance.

[B]Do any body have some idea…

Many thanks in advance…[/B]

if you just wanna “echo” it. you can try this code:


<?php
$x = date("md");
function add_nol($number,$add_nol) {
   while (strlen($number)<$add_nol) {
       $number = "0".$number;
   }
   return $number;
}

//usage..
for($y=10;$y<=20;$y++){
	echo $x."".add_nol($y,3)."<br />";
}
?>

Actually, i don’t exactly get your idea :smiley: . so CMIIW

are you looking for real dates or a number incrementer?

Try www.php.net/range for a range of numbers, and look up pad() for infilling zeros

Thanks guys for ur help…
I think u dont understand my q…
its simple,
For example i want to store this in db:(0107001)
here 01 is month,07 is date,001 is number(but it increments on each insertion
to the db like 002,003,004)…
it will loo like this(0107001)(0107002)(0107003).Also like this may be
(0107-001)…
for date i know to create this:

$rtno = date(“md”,but here i want some function to start number from 001
and increment it on each insertion)…

Hopefully u people will understand me now…

Thanks…

If you’re doing all your inserts inside a single execution of a single script, then your best bet would be to use a variable that you initialize to 1 and then increment after each insert; see [fphp]str_pad[/fphp] for how to turn this into a 0-padded 3-digit number like what you’re asking for.

If you’re doing this across multiple executions (which I find to be the most likely scenario), then there really isn’t an easy way to do this. You could adopt the suggestion I made above and then write your next value to a file to be loaded upon the next batch of inserts, but by far the easier approach would be to use an auto_increment column in your database and store your date in a separate DATE column.

Thanks Kromey for ur suggestion…

U write to auto_increment the field in db And store date in separate
field… u r right here…
i already have these…
But the thing for which i am searching is like a receipt No:
it is to issue a customer a receipt with curdate and his rtno like(001,002)…
which will mixed and create a rtno like this (0107001)…or this (0107-001)

i hope u understand what i want to say here…

I’ve never seen receipt numbers that include the date like that, I’ve just seen sequentially numbered receipts. I don’t know if this will work for you, but worth a shot:


$rtno = date("md").str_pad($id, 3, "0", STR_PAD_LEFT);

Where $id is your auto_increment column from your database. This of course won’t restart at 1 each day, but otherwise it does match what you’ve described I believe.

Thanks kromey its fine and works well…
But one thing i want is in php their any function to increment a number…
U mention in ur post $id but i dont want to use it,
is their any variable which i pass here,that variable will incremented…

THANKS

To increment a number in PHP:


$i = 1;
$i++;
echo $i; //will print '2'

Thanks Kromey,
its ok but how to change it to increment on each insertion…
bcoz now it store one value on each insertion.
Dont mind please…

OR: any idea about the $id how to retrieve that id from db…bcoz in my php script no id…
it is auto icrmented in the db, and primary key…
Thanks…

There is no elegant way to do this in PHP. The best you can come is to store your last value in a database or a file, then load that up on each script execution. Of course, then you’ll have to deal with race conditions (what happens if the script executes twice simultaneously?), so now you have to deal with mutexes… it gets really messy really fast.

Thank you very much for ur help…