Skip to content

Instantly share code, notes, and snippets.

@cuonggt
Created February 27, 2016 18:24
Show Gist options
  • Select an option

  • Save cuonggt/b39ac7b513d518789e68 to your computer and use it in GitHub Desktop.

Select an option

Save cuonggt/b39ac7b513d518789e68 to your computer and use it in GitHub Desktop.
<?php
function generateCode($id) {
$codeStr = (1000000000 + $id) . generateRandomSerialNumber(4);
return $codeStr . generateCheckDigit($codeStr);
}
function generateRandomSerialNumber($length)
{
$vowels = '0123456789';
$consonants = '0123456789';
$password = '';
$alt = time() % 2;
for ($i = 0; $i < $length; $i++)
{
if ($alt == 1)
{
$password .= $consonants[(rand() % strlen($consonants))];
$alt = 0;
}
else
{
$password .= $vowels[(rand() % strlen($vowels))];
$alt = 1;
}
}
return $password;
}
function generateCheckDigit($codeStr) {
$odd_total = 0;
$even_total = 0;
$codeLength = strlen($codeStr);
for ($i =0; $i < $codeLength; $i++)
{
if ((($i+1)%2) == 0) {
$even_total += $codeStr[$i];
} else {
$odd_total += $codeStr[$i];
}
}
$sum = (3 * $odd_total) + $even_total;
$check_digit = $sum % 10;
return ($check_digit > 0) ? 10 - $check_digit : $check_digit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment