Convert ID Number to Birthdate, gender and age as of 31/12 of current yr

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • neelsfer
    Contributor
    • Oct 2010
    • 547

    Convert ID Number to Birthdate, gender and age as of 31/12 of current yr

    I am a new, "trying to be" PHP Html and MySqli programmer coming from Ms Access and VB background. Can somebody please assist, as I am stuck and sweating with these calculations.An y suggestions on how to populate the relevant input text boxes in realtime when adding an IDNo to the screen in input textbox called IDNO? Currently it only populate the mysqli database on pressing the submit button.

    Example Identity number = 63021854485519

    The Birthdate from this = dd/mm/yyyy = 18/02/1963
    In PHP this works, but it does not populate Birthdate input text box in realtime as IDNo is added.
    Code:
    function getBirthdateFromIdentity($identity) {
        // substring identity to get bday
        $date = substr($identity, 0, 6);
    
        // use built-in DateTime object to work with dates
        $date = \DateTime::createFromFormat('ymd', $date);
        $now  = new \DateTime();
    
        // compare birth date with current date: 
        // if it's bigger bd was in previous century
        if ($date > $now) {
            $date->modify('-100 years');
        }
    
        return $date;
    The 7th digit indicates male or female ie 0-4 = female and 5-9 = male , So
    this person's Id No indicates its a Male as 7th digit is 5 (between 5-9)
    In PHP this works but does not populate gender input textbox in realtime as IDNo is added
    Code:
    function getGenderFromIdentity($identity) {
        // substring gender data and convert it to int
        $gender = (int) substr($identity, 6, 1);
        return ($gender >= 0 && $gender <= 4) ? 'Female' : 'Male';
    }
     */
    Lastly i also need to calculate the AGE of this person from the birthdate as of 31 December of current year (NOT TODAY as this code shows)
    In PHP as of today i use
    Code:
    function getAgeFromBirthday(\DateTime $birthdate) {
        $date = new DateTime();
        $interval = $date->diff($birthdate);
        return $interval->y;
    }

    (In VB to calculate birthdate from ID No I use ---> DOB=Mid([idno],5,2) & "/" & Mid([idno],3,2) & "/" & Mid([idno],1,2) Require as dd/mm/yyyy.
    (PHP

    In VB to calculate Gender I use --> Gender=IIf(Mid([idno],7,1) Between 0 And 4,"Female","Mal e").)

    To summarize how can i populate the birthdate, gender and age on screen to the relevant input textboxes, after entering the IDNO onscreen. Pls advise as this causing nightmares in my life.
    Attached Files
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Any suggestions on how to populate the relevant input text boxes in realtime
    for realtime actions (i.e. user interactions) you need JavaScript. PHP always requires a round trip to the server.

    Comment

    • neelsfer
      Contributor
      • Oct 2010
      • 547

      #3
      I suppose all the code needs to be rewritten from PHP to Javascript? One cannot only adjust it?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        One cannot only adjust it?
        No matter what you adjust in PHP, you will never get immediate user interaction.

        Mind, PHP only runs on the server. if you want direct user interaction you must use some JS. Although you can use JS to let PHP do stuff (=> AJAX), the type of calculation can be done in JS as well as it is quite simple.

        Comment

        Working...