Home  >  Article  >  Backend Development  >  PHP example code to calculate age based on ID number_PHP tutorial

PHP example code to calculate age based on ID number_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:40:421091browse

Copy code The code is as follows:

function getAgeByID($id){

/ / Only after this year’s birthday is one more year old
If(empty($id)) return '';
$date=strtotime(substr($id,6,8));
// Get the time stamp of the date of birth
$ TODAY = StrTotime ('TODAY');
// Get today's timestamp
$ Diff = Floor (($ today-$ date)/86400 /365);
//Get the approximate number of years between the two dates

//Add this number of years to strtotime to get the timestamp of that day and compare it with today’s timestamp
       =strtotime(substr($id,6,8).' +'.$diff.'years')>$today?($diff+1):$diff;

Return $age;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/714950.htmlTechArticleCopy the code as follows: ?php function getAgeByID($id){ //It’s only after this year’s birthday 1 more year old if(empty($id)) return ''; $date=strtotime(substr($id,6,8)); //Get the date of birth...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn