Search This Blog

Labels

adobe (1) android (3) apache (3) aviation (1) bash (2) chrome (2) composer (1) cookery (3) dev (3) dodanperks (1) extensions (1) facebook (2) firefox (1) git (3) grafana (1) guzzle (1) headaches (12) htaccess (1) html5 (2) jquery (2) lamp (1) life hacks (10) linux (29) mysqli (2) native (1) opera (2) php (10) railfanning (1) reactjs (3) reactnative (1) servers (11) sinhala (1) smartphones (2) snap (1) sound (1) tech (22) troubleshoots (5) ubuntu (30) unicode (4) virtualbox (1) wamp (2) web (11) windows (4) wordpress (3) youtube (2) කෑම (3)

Friday, 4 July 2014

Calculate your b'day from ID number with php

<?php
if(isset($_REQUEST['id_no'])){
 $id_no=$_REQUEST['id_no'];
}
else
 $id_no="";
?>

<form name="idcalc_form" method="get" action="">
<input type="text" name="id_no" value="<?php echo $id_no;?>" pattern="[0-9]{9}[V]" autofocus placeholder="NIC number"/>
<input type="submit" name="idcalc">
</form>
<?php

if(isset($_REQUEST['id_no']) && isset($_REQUEST['idcalc'])){
 $year=substr($id_no,0,2);
 $days=substr($id_no,2,3);

 function checkNum($between) {
   if($between=='' || $between=='000' || ($between > 366 && $between < 501) || ($between > 866)) {
  throw new Exception("Value must be between 1 - 366 and 501 - 866 (inclusive)");
   }
   return true;
 } 
 try {
   checkNum($days);
  if($days<=366){$gender='Male';}else{$gender='Female'; $days=$days-500;}

  if($days>='001' && $days<='031'){
   $month=array('Jan',1);
  }elseif($days>='032' && $days<='060'){
   $month=array('Feb',32);
  }elseif($days>='061' && $days<='091'){
   $month=array('Mar',61);
  }elseif($days>='092' && $days<=121){
   $month=array('Apr',92);
  }elseif($days>=122 && $days<=152){
   $month=array('May',122);
  }elseif($days>=153 && $days<=182){
   $month=array('Jun',153);
  }elseif($days>=183 && $days<=213){
   $month=array('Jul',183);
  }elseif($days>=214 && $days<=244){
   $month=array('Aug',214);
  }elseif($days>=245 && $days<=274){
   $month=array('Sep',245);
  }elseif($days>=275 && $days<=305){
   $month=array('Oct',275);
  }elseif($days>=306 && $days<=335){
   $month=array('Nov',306);
  }elseif($days>=336 && $days<=366){
   $month=array('Dec',336);
  }
  else $month=array('bogus',0);
  $date=$days-$month[1]+1;
  echo $year.' '.$month[0].' '.$date.' '.$gender;
 }
 catch(Exception $e) {
   echo 'Error: ' .$e->getMessage();
 }
}
?>

No comments:

Post a Comment