Menghitung Umur dengan PHP

Bagaimana cara menghitung umur seseorang, jika kita tahu tanggal lahirnya?
Script berikut bukan hasil karya sendiri, tapi dapat dari hasil googling.

/** 
Calculate Age

With this function you can calculate the age of a person

Example:
echo "Age is: " . calc_age("1984-07-05");

Result will be (23 Feb 2005) = "Age is: 20"

calculate years of age (input string: YYYY-MM-DD)

*/
function calc_age($birthdate) {
    list($year,$month,$day) = explode("-",$birthdate);
    $year_diff  = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff   = date("d") - $day;
    if ($month_diff < 0) $year_diff--;
        elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
    return $year_diff;
}

echo calc_age("1980-07-02");

Semoga berguna.

***

Sumber:

Published by Eric Gunawan

Happiness Engineer. WordPress Ambassador. Remote Worker. Soccer News Follower. Movie Lover. Proud Father. Lucky Husband.

2 thoughts on “Menghitung Umur dengan PHP

Leave a comment