Saturday, May 21, 2011

birth date to year calculation in php

Note:- If you want to change formate of date for example YYYY-MM-DD to MM-DD-YYYY then change in list() function parametter and pass same formate parameter end of birthday function.

<\?php
function birthday($birthday)
{
    list($year,$month,$day) = explode("-",$birthday);

    //list($day,$month,$year) = explode("-",$birthday);

    $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 birthday("YYYY-MM-DD");

echo birthday("1982-11-10");

//echo birthday("11-01-1985");
?>