Skip to content
Advertisement

PHP: Generate unique random int number based on an id

I’m currently searching for a good way to generate a unique random number for SEPA transactions (End-to-End reference) with 27 digits in PHP. My requirements are:

  • Must be unique
  • Only int values, no letters
  • Length of 27 digits
  • Use an user id and time to make the ID unique

I’ve tried this solution here but this only gives me a string with letters and numbers:

md5( uniqid( mt_rand(), true ) );

Does anyone has a lightweight solution or idea?

Advertisement

Answer

echo $bira = date('YmdHis').rand(1000000000000,9000000000000);
echo "<br/>";
echo strlen($bira);

Add the time stamp in the front, so it will be unique always.

OR echo $bira = time().rand(10000000000000000,90000000000000000);

outoput:

201901220142532979656312614

27

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement