Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all I would go for an auto increment too. Even if you choose to use a code as mentioned (month + random stuff) I would advise to also use a numeric (int/bigint) auto increment id. You should use this id 'below the surface' for your code to communicate with the database (referencing/looking up records).</p> <p>So for the 'month + random stuff' identifier I wouldn't advice to use anything with <code>uniqid()</code> for it is based on microseconds and not unique if two transactions would take place at the same microsecond. Also I would discourage the use of <code>md5()</code>, because this function would be very useful for hashing, but slows your code down a lot if you'd only use it for generating chars.</p> <p>Also your own code suggestion generates 4 random chars that could be 0-9 or a-f (hexadecimal) which gives you 16 possibilities. 4 chars of 16 possibilities gives you 16x16x16x16 = 65,536 unique strings. It would be highly possible that when rendering 10,000 of these strings you'd get duplicates.</p> <p>I would advice to generate a random string yourself from the chars 0-9, a-z and A-Z, which gives you 36 possibilities per char and with 4 chars gives you 36x36x36x36 = 1,679,616 unique strings. More chars gives you less probability for duplicates (collision). You could do such thing like this:</p> <pre><code>function randString($length = 4) { $randChars = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $randString = ''; while (strlen($randString) &lt; abs(intval($length))) { $randString .= substr($randChars, rand(0, strlen($randChars)-1), 1); } return $randString; } $id = date('m') . randString(4); </code></pre> <p>I would also leave out the dash ('<code>-</code>') between the month and the random chars because this saves you 1 char and you know the first two chars will always be the number of the month.</p> <p><strong>Edit: done some tests</strong></p> <p>I've done some testing for the collision: I've rendered 10,000 random strings a hundred times and checked the uniqueness every time. Done this with length 3, 4 (as you did), 5 and 6. So here are the results:</p> <pre><code>-------- Length 3 ------------ Minimal duplicates: 170 Maximum duplicates: 241 Average duplicates: 207.05 -------- Length 4 ------------ Minimal duplicates: 0 Maximum duplicates: 8 Average duplicates: 3.19 -------- Length 5 ------------ Minimal duplicates: 0 Maximum duplicates: 2 Average duplicates: 0.05 -------- Length 6 ------------ Minimal duplicates: 0 Maximum duplicates: 0 Average duplicates: 0 </code></pre> <p>Absolute 0% chance of collision can't be ruled out as also pointed out by <a href="https://stackoverflow.com/questions/15903606/php-unique-id-within-a-month#comment22648410_15903739">Mark B</a> referring to the Birthday Problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload