PHP Age Calculator

This is a simple PHP script which allows you to calculate age of person based on the specified date. This tiny piece of code can be very useful while creating web applications where you want to calculate persons age using his date of birth.

Quick Tip : Clean and SEO Friendly URLs with PHP

<?php
/**
 * Simple PHP age Calculator
 * 
 * Calculate and returns age based on the date provided by the user.
 * @param   date of birth('Format:yyyy-mm-dd').
 * @return  age based on date of birth
 */
function ageCalculator($dob){
	if(!empty($dob)){
		$birthdate = new DateTime($dob);
		$today   = new DateTime('today');
		$age = $birthdate->diff($today)->y;
		return $age;
	}else{
		return 0;
	}
}
$dob = '1992-03-18';
echo ageCalculator($dob);
?>

 

1 Comments

  1. Accurate age calculator 😀

    function computeAge($year, $month, $day){

    $iBday = mktime(0,0,0,$month,$day,$year);

    $iAge = (floor((time()-$iBday)/31556926));

    if( (int)sprintf(“1%02s%02s”, $month, $day) < (int)date("1md", time()) ){
    $iAge–;
    }

    return $iAge;
    }

    Reply

Leave a Comment.