Extragere date din cnp (php)

Va propun un cod de extragere de date specifice dintr-un CNP (13 cifre). Aceste date ar putea fi sex, an,luna,zi, data nasterii,judet,nr si crc.

function cnp_extract($cnp, $extract_type)
{
  if (strlen($cnp) == 13)
  {
    $sex = $cnp[0];
    $bd = substr($cnp, 1, 6);
    if ($sex == 1 || $sex == 2) { $sy = 19; }
    elseif ($sex == 3 || $sex == 4) { $sy = 18; }
    elseif ($sex == 5 || $sex == 6) { $sy = 20; }
    $year = $sy.$bd[0].$bd[1];
    $month = $bd[2].$bd[3];
    $day = $bd[4].$bd[5];
    $birthday = strtotime("$year-$month-$day");
    $judet = $cnp[7].$cnp[8];
    $nr = $cnp[9].$cnp[10].$cnp[11];
    $crc = $cnp[12];
    if(isset($$extract_type)
    {
      return $$extract_type;
    }
    else
    {
      return false;
    }
  }
  else
  {
    return false;
  }
}

Astfel callul : cnp_extract($cnp, 'birthday') va intoarce data nasterii.

Leave a Reply