function get_age($birthday_day, $birthday_month, $birthday_year)
{
$user_age = date('Y') - $birthday_year;
if ( mktime(0, 0, 0, $birthday_month, $birthday_day, date('Y')) > time() )
{
$user_age--;
}
return $user_age;
}
class hrwsPassport {
var $lastresult = array();
function pak_check($aArr) {
$res = array("valid" => false, "birthday" => 0, "age" => "", "validto" => 0, "error" => 1);
$CodeA = trim($aArr[0]);
$CodeB = trim($aArr[1]);
$CodeC = trim($aArr[2]);
$CodeD = trim($aArr[3]);
if (strlen($CodeA) > 10)
{
$CodeA = substr($CodeA, 0, 10);
}
if (strlen($CodeB) > 7)
{
$CodeB = substr($CodeB, 0, 7);
}
if (strlen($CodeC) > 7)
{
$CodeC = substr($CodeC, 0, 7);
}
if (strlen($CodeD) > 1)
{
$CodeD = substr($CodeD, 0, 1);
}
if ((strlen($CodeA) != 10) || (strlen($CodeB) != 7) || (strlen($CodeC) != 7) || (strlen($CodeD) != 1))
{
return $res;
}
if ($this->pak_getchecksum(substr($CodeA,0,-1)) != substr($CodeA, -1))
{
return $res;
}
if ($this->pak_getchecksum(substr($CodeB,0,-1)) != substr($CodeB, -1))
{
return $res;
}
if ($this->pak_getchecksum(substr($CodeC,0,-1)) != substr($CodeC, -1))
{
return $res;
}
if ($this->pak_getchecksum($CodeA.$CodeB.$CodeC) != $CodeD)
{
return $res;
}
$res['valid'] = true;
$year = (int)(substr($CodeB,0,2));
if (substr($CodeB,0,6) <= strftime("%y%m%d"))
{
$year += 2000;
}
else
{
$year += 1900;
}
$res['byear'] = $year;
$res['bmonth'] = (int)(substr($CodeB,2,2));
$res['bday'] = (int)(substr($CodeB,4,2));
if ($year >= 1970)
{
$res['birthday'] = mktime(0,0,0,$res['bmonth'], $res['bday'], $year);
}
else
{
$res['birthday'] = -1;
}
$res['validto'] = mktime(0,0,0,(int)(substr($CodeC,2,2)), (int)(substr($CodeC,4,2)), (int)(substr($CodeC,0,2)));
$res['age'] = date('Y')-$year;
if (date('m') < $res['bmonth'])
{
$res['age']--;
}
else if ((date('m') == $res['bmonth']) && (date('d') < $res['bday']))
{
$res['age']--;
}
if (time() > ($res['validto'] + (60*60*24)))
{
$res['valid'] = false;
$res['error'] = -1;
}
else
{
$res['error'] = 0;
}
$this->lastresult = $res;
return $res;
}
// private function
function pak_getchecksum($aCode) {
// Calculate and return the checksum
$multi = array(7,3,1);
$i = 0;
$sum = 0;
for ($c = 0; $c < strlen($aCode); $c++)
{
$sum += $multi[$i] * (int)(substr($aCode,$c,1));
$i++;
if ($i > 2)
{
$i = 0;
}
}
return ($sum%10);
}
}
?>