Skip to content

Instantly share code, notes, and snippets.

@mariancerny
Created April 14, 2017 21:15
Show Gist options
  • Save mariancerny/474f6eab64cef87cf17156b958a8440f to your computer and use it in GitHub Desktop.
Save mariancerny/474f6eab64cef87cf17156b958a8440f to your computer and use it in GitHub Desktop.
// Kontrola rodného čísla.
// @author: Marián Černý
private function is_RC_valid($value) {
if (!preg_match('@^([0-9][0-9])([0-9][0-9])([0-9][0-9])/?([0-9]{3})([0-9])?$@', $value, $regs))
return false;
list($expression, $year, $month, $day, $ext) = $regs;
$c = isset($regs[5]) ? $regs[5] : "";
if (strlen($value) == 9) {
if ($year >= 54)
// Nemame kontrolnu cislicu a rok je 54+.
return false;
$year += 1900;
} else {
// If we used 10 digits RC number, we would have to use bcmod()
// instead of the modulo operator (%), because the RC number is
// out of range of integer on 32 bit systems.
$mod = "$year$month$day$ext" % 11;
if ($mod == 10)
$mod = 0;
if ($mod != $c)
// Nesedi kontrolna cislica.
return false;
if ($year < 54)
$year += 2000;
else
$year += 1900;
}
// K mesiacu je pripocitane 50 ak ide o zenu.
// Od roku 2004 moze byt pripocitane 20 (muz) alebo 70 (zena).
if ($month > 70 && $year >= 2004)
$month -= 70;
else if ($month > 50)
$month -= 50;
else if ($month > 20 && $year > 2003)
$month -= 20;
// Cudzinci maju k dnu pripocitane 50.
if ($day > 50 && $year >= 1954)
$day -= 50;
if (!checkdate($month, $day, $year))
// Datum nedava zmysel.
return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment