How can we calculate age in PHP?
Wednesday, October 14, 2009 14:06Posted in category PHP Interview Questions
No Comments
Simple Age Calculation in PHP:
<?php
// make sure to set timezone //
date_default_timezone_set('Europe/London');
function getAge( $dob, $tdate ) // int $dob, int $tdate, int years
{
$age = 0;
while( $tdate > $dob = strtotime('+1 year', $dob)){
++$age;
}
return $age;
}
### Date of Birth ###
$dob = strtotime("april 20 1966");
### Current Date ***/
$tdate = strtotime("june 2 2009");
### show the date ***/
echo getAge( $dob, $tdate );
?>
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.


