Last active
November 3, 2021 06:25
-
-
Save kebalicious/5d2e185729b505b363b4604e00581a3a to your computer and use it in GitHub Desktop.
MyKad Validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MyKad | |
$this->myKad = "010203-04-1234"; | |
$this->myKad = str_replace(['-', ' '], '', $this->myKad); | |
if (!is_numeric($this->myKad)) { | |
return "not valid mykad no"; | |
} elseif (strlen($this->myKad) != 12) { | |
return "mykad no should contains 12 digits"; | |
} | |
$head = substr($this->myKad, 0, 6); | |
$day = (int) substr($head, 4, 2); | |
$month = (int) substr($head, 2, 2); | |
$parseYear = (int) substr($head, 0, 2); | |
$year = $parseYear + ($parseYear >= 30 ? 1900 : 2000); | |
try { | |
Carbon::parse($day . '-' . $month . '-' . $year); | |
} catch (\Exception $e) { | |
echo "xx"; | |
} | |
// separation | |
$this->head = substr($this->myKad, 0, 6); | |
$this->body = substr($this->myKad, 6, 2); | |
$this->tail = substr($this->myKad, 8, 4); | |
// tarikh lahir | |
$this->day = (int) substr($this->head, 4, 2); | |
$this->month = (int) substr($this->head, 2, 2); | |
$parseYear = (int) substr($this->head, 0, 2); | |
$this->year = $parseYear + ($parseYear >= 50 ? 1900 : 2000); | |
// Can use this to get DOB | |
// Option 1 | |
$this->birthdate = Carbon::parse($this->day . '-' . $this->month . '-' . $this->year); | |
// Option 2 | |
$date = date_create($this->day . '-' . $this->month . '-' . $this->year); | |
$newDate = date_format($date, "d/m/Y"); | |
// jantina | |
$lastDigit = (int) substr($this->tail, -1); | |
$this->gender = ($lastDigit % 2) ? 'male' : 'female'; | |
// return $this->gender; | |
// umur | |
$this->age = $this->birthdate->age; | |
// return $this->age; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment