创建唯一帐单标识符

I would like to create a unique billing identifier/number. I want it to be a combination of a constant string, year and an additional number. For example "ABC-2013-100001". How can I do that so I will be definitely sure that the billing identifier is unique? I was thinking about maintaing a billing_id like a primary key that would be unique and autoincrement. Then I would just select max(billing_id) to get the last one add 1 to it and the prefix "ABC-2013" and then insert it into the table. The problem is that this is a 3 step way. Select, do something, insert. I am afraid that it might happen something like, when 2 users will do this at the same time, every one selects the same number, so technically they insert the same identifier. Am I wrong and overreacting or is this kind of generating unique ids common? Should I take a different approach?

EDIT

Ok so I did something like this:

$data = array(
    'billing_identifier' => 'law-'.$year."-".$this->db->insert_id()
);
// inserting into billing_identifiers
    $this->db->insert('billing_identifiers', $data);

    $bill_id = $this->db->insert_id();

but the $this->db->insert_id() is 0 at the beginning which makes sense and after calling the insert function it gets the last id. Isnt there a way I can do that in one shot?

Just use this, it will give you the primary id.

<?php
$con=mysqli_connect("127.0.0.1","user","pass","db");
//query here
$id = mysqli_insert_id($con);

$bill_id = 'ABC-2013-'. $id;

?>

changed because of "doesn't fit the question"...

There is a MySQL function for getting the last inserted ID, and one to pad a string with for example zeroes:

CONCAT(
    'ABC-2013-',
    LPAD(
         LAST_INSERT_ID()+1,
         6,
         '0'
    )
)

LAST_INSERT_ID()+1 is the id currently being inserted.

LPAD(str, length, pad) pads the new ID with zeroes in front of it.

CONCAT(..., ...) combines to values into a string.

Insert that into the billing identifier column.

you can also use

strtime(d/y/m) time object