Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thirteen's avatar

Extract Gender and Birth date from ID Number

Hello, I need to extract birth date and gender from the user i.d card number.

User Table

Autofill

The ID Card consist of 13 digits as follows:

  • 1 digit for sex: 1 male, 2 female.
  • 6 digits for birth date in the format YYMMDD
  • 2 digits for place of birth (the code for the county)
  • 3 other digits
  • 1 control digit

Example: 2690627131268 ( female, 27-06-1969 )

How can i do that ? Thanks!

0 likes
3 replies
tomopongrac's avatar

You can convert integer to string with with (string) $integer

and then get character which you wont with function substr()

http://php.net/manual/en/function.substr.php

$integer = 2690627131268;

$string = (string) $integer;


if (substr($string, 0, 1) == 2)
{
    echo "female";
}
else {
    echo "male";
}

echo ", " . substr($string, 5, 2) . "-" . substr($string, 3, 2) . "-19" .  substr($string, 1, 2);
simondavies's avatar

You can use the following:

$userinfo = '2690627131268';
$DOB = substr($userinfo, 1, 6);
$Gender = $userinfo[0];
$place = substr($userinfo, 7, 2);

etc

lostdreamer_nl's avatar

Class IdConverter
{
    private $id;

    public function __construct($id)
    {
        $this->id = $id;
        if (!$this->checkControlDigit()) {
            Throw new Exception('the id is invalid: ' . $id);
        }
    }

    public function getSex()
    {
        return $this->id[0] == 1 ? 'M' : 'F';
    }

    public function getDate()
    {
        $date = substr($this->id, 1, 6);
        $date = str_split($date, 2);
        $currentCentury = floor(date('Y') / 100);
        if ($date[0] > date('y')) {
            // year is in last century
            $date[0] = ($currentCentury - 1) . $date[0];
        } else {
            $date[0] = $currentCentury . $date[0];
        }
        return implode("-", $date);
    }

    public function getPlaceOfBirth()
    {
        return substr($this->id, 7, 2);
    }

    private function checkControlDigit()
    {
        // replace with actual check
        return true;
    }

}
$info= new IdConverter(2690627131268);

echo $info->getSex();
echo $info->getDate();
echo $info->getPlaceOfBirth();
4 likes

Please or to participate in this conversation.