Skip to content

Instantly share code, notes, and snippets.

@xnekv03
Forked from dg/verifyIC.php
Last active December 29, 2020 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xnekv03/7d684df577a483d8b7734dafb8291e3d to your computer and use it in GitHub Desktop.
Save xnekv03/7d684df577a483d8b7734dafb8291e3d to your computer and use it in GitHub Desktop.
Ověření rodného čísla a IČ
<?php
function verifyIC($ic)
{
// be liberal in what you receive
$ic = preg_replace('#\s+#', '', $ic);
// má požadovaný tvar?
if (!preg_match('#^\d{8}$#', $ic)) {
return FALSE;
}
// kontrolní součet
$a = 0;
for ($i = 0; $i < 7; $i++) {
$a += $ic[$i] * (8 - $i);
}
$a = $a % 11;
if ($a === 0) {
$c = 1;
} elseif ($a === 1) {
$c = 0;
} else {
$c = 11 - $a;
}
return (int) $ic[7] === $c;
}
<?php
function verifyRC($rc)
{
// be liberal in what you receive
if (!preg_match('#^\s*(\d\d)(\d\d)(\d\d)[ /]*(\d\d\d)(\d?)\s*$#', $rc, $matches)) {
return FALSE;
}
list(, $year, $month, $day, $ext, $c) = $matches;
if ($c === '') {
$year += $year < 54 ? 1900 : 1800;
} else {
// kontrolní číslice
$mod = ($year . $month . $day . $ext) % 11;
if ($mod === 10) $mod = 0;
if ($mod !== (int) $c) {
return FALSE;
}
$year += $year < 54 ? 2000 : 1900;
}
// k měsíci může být připočteno 20, 50 nebo 70
if ($month > 70 && $year > 2003) {
$month -= 70;
} elseif ($month > 50) {
$month -= 50;
} elseif ($month > 20 && $year > 2003) {
$month -= 20;
}
// kontrola data
return checkdate($month, $day, $year);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment