You cannot rely on rand() . You might create a duplicate (Quite rarely for rand(0,9999) to create a duplicate, but it will be at some point).
So instead of going to rand() just create an incremental value (say starting at 1) and add to your generated date.
The next time you restore a new identifier, take this incremental value (say, if you saved it somewhere) should be 1 right?), Increase it and add it to the new date.
Not a perfect solution .. (Critics are welcome)
You can use uniqid in combination with sha-1 and time and do substr() for them for the first 4 characters.
<?php $today = date("Ymd"); $rand = strtoupper(substr(uniqid(sha1(time())),0,4)); echo $unique = $today . $rand;
OUTPUT :
201403094B3F
Shankar damodaran
source share