Sunday, June 10, 2012

Calculate Age from Birth Day php


Using php You could do lot of calculations.From this tutorial i am going to introduce you popular and an interesting type of calculations.Some of calculations, I have introduced you in mysql php examples.Here you have to enter your birth days in this format of date "1965-06-12". The input field is bellow.


php codes to calculate age from birth day is as follows

<?php
$birthday=$_POST['birth_day'];
function birthday($birthday){
    $age = strtotime($birthday);
    if($age === false){
        return false;
    }
    list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age));
    $now = strtotime("now");
    list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now));
    $age = $y2 - $y1;
    if((int)($m2.$d2) < (int)($m1.$d1))
        $age -= 1;
    return $age;
}

echo birthday($birthday);
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
<table>
<tr><td><input type="text" name="birth_day"></td></tr>
<tr><td><input type="submit" value="Enter"></td></tr>
 </form>
</table>

Using bellow scripts you will get year,month and dates also

<?php
$bday=$_POST['birth_day'];
function get_Age_difference($start_date,$end_date){
    list($start_year,$start_month,$start_date) = split('-', $start_date);
    list($current_year,$current_month,$current_date) = split('-', $end_date);
     $result = '';

   for($x=1 ; $x<=12 ; $x++){

        $dim[$x] = date('t',mktime(0,0,0,$x,1,date('Y')));

    }

    $m = $current_month - $start_month;
    $d = $current_date - $start_date;
    $y = $current_year - $start_year;

 if($d < 0) {

        $today_day = $current_date + $dim[$current_month];
        $today_month = $current_month - 1;
        $d = $today_day - $start_date;
        $m = $today_month - $start_month;
        if(($today_month - $start_month) < 0) {

            $today_month += 12;
            $today_year = $current_year - 1;
            $m = $today_month - $start_month;
            $y = $today_year - $start_year;

        }

    }

   if($m < 0) {

        $today_month = $current_month + 12;
        $today_year = $current_year - 1;
        $m = $today_month - $start_month;
        $y = $today_year - $start_year;

        }

  if($y < 0) {

        die("Start Date Entered is a Future date than End Date.");

    } else {

        switch($y) {

            case 0 : $result .= ''; break;
            case 1 : $result .= $y.($m == 0 && $d == 0 ? ' year old' : ' year'); break;
            default : $result .= $y.($m == 0 && $d == 0 ? ' years old' : ' years');

        }

        switch($m) {

            case 0: $result .= ''; break;
            case 1: $result .= ($y == 0 && $d == 0 ? $m.' month old' : ($y == 0 && $d != 0 ? $m.' month' : ($y != 0 && $d == 0 ? ' and '.$m.' month old' : ', '.$m.' month'))); break;
            default: $result .= ($y == 0 && $d == 0 ? $m.' months old' : ($y == 0 && $d != 0 ? $m.' months' : ($y != 0 && $d == 0 ? ' and '.$m.' months old' : ', '.$m.' months'))); break;

        }

        switch($d) {

            case 0: $result .= ($m == 0 && $y == 0 ? 'Today' : ''); break;
            case 1: $result .= ($m == 0 && $y == 0 ? $d.' day old' : ($y != 0 || $m != 0 ? ' and '.$d.' day old' : '')); break;
            default: $result .= ($m == 0 && $y == 0 ? $d.' days old' : ($y != 0 || $m != 0 ? ' and '.$d.' days old' : ''));

        }

    }

    return $result;

}

$date_difference= get_Age_difference($bday,date("Y-m-d"));
echo $date_difference;
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
<table>
<tr><td><input type="text" name="birth_day"></td></tr>
<tr><td><input type="submit" value="Enter"></td></tr>
</form>
</table>
Next


No comments:

Post a Comment