Created
January 28, 2016 13:58
-
-
Save s22su/344afbc91ff8a87ebe49 to your computer and use it in GitHub Desktop.
Generate Estonian personal (id) code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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