How to calculate age in PHP
Posted on August 29, 2008, Filled under PHP,
Bookmark it
Here’s a simple php function that calculates the age of someone, in years.
<?php
/*
Credits: http://www.bitrepository.com
URL: http://www.bitrepository.com/web-programming/php/simple-age-calculator.html
*/
function determine_age($birth_date)
{
$birth_date_time = strtotime($birth_date);
$to_date = date('m/d/Y', $birth_date_time);
list($birth_month, $birth_day, $birth_year) = explode('/', $to_date);
$now = time();
$current_year = date("Y");
$this_year_birth_date = $birth_month.'/'.$birth_day.'/'.$current_year;
$this_year_birth_date_timestamp = strtotime($this_year_birth_date);
$years_old = $current_year - $birth_year;
if($now < $this_year_birth_date_timestamp)
{
/* his/her birthday hasn't yet arrived this year */
$years_old = $years_old - 1;
}
return $years_old;
}
// You can write about any English textual datetime description
$birth_date = '1 May 1980';
$age = determine_age($birth_date);
echo $age;
?>Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!
- August 29, 2008
- article by Gabriel C.
- 2 comments
Related Posts
PHP: Calculate the Size, Number of Files & Folders of a Directoryat September 24, 2008 with 3 comments
Calculate percent from valueat August 31, 2008 with 2 comments
PHP: How to calculate the size of a fileat September 11, 2008 with 2 comments

2 Replies to "How to calculate age in PHP"
February 12, 2009 at 8:12 PM
Thanks for the code!!
October 20, 2009 at 11:48 PM
HI
this code is not working for AGE before
1970 year
it is working only for the YEAR after 1970
please check 1 ce