Skip to content

Instantly share code, notes, and snippets.

@s22su
Created January 28, 2016 13:58
Show Gist options
  • Select an option

  • Save s22su/344afbc91ff8a87ebe49 to your computer and use it in GitHub Desktop.

Select an option

Save s22su/344afbc91ff8a87ebe49 to your computer and use it in GitHub Desktop.
Generate Estonian personal (id) code
<?php
function cenerateDummyIdCode() {
$multiplier = [
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1],
[3, 4, 5, 6, 7, 8, 9, 1, 2, 3],
];
// get random date from 1970-01-28 to 1990-01-28
$birthday = mt_rand(2382512, 633534512);
$gender = mt_rand(3, 4);
$random = mt_rand(100, 999);
$code = $gender . date('ymd', $birthday) . $random;
$total = 0;
for ($i = 0; $i < 10; $i ++) {
$total += (int) substr($code, $i, 1) * $multiplier[0][$i];
}
$modulus = $total % 11;
$total = 0;
if (10 === $modulus) {
for ($i = 0; $i < 10; $i ++) {
$total += (int) substr($code, $i, 1) * $multiplier[1][$i];
}
$modulus = $total % 11;
if (10 === $modulus) {
$modulus = 0;
}
}
$code .= $modulus;
return $code;
}
echo cenerateDummyIdCode();
echo '<br>';
echo cenerateDummyIdCode();
echo '<br>';
echo cenerateDummyIdCode();
echo '<br>';
echo cenerateDummyIdCode();
echo '<br>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment