Home > Backend Development > PHP Tutorial > php根据生日计算年龄的方法

php根据生日计算年龄的方法

PHP中文网
Release: 2023-02-28 09:42:01
Original
2256 people have browsed it

本文实例讲述了php根据生日计算年龄的方法。分享给大家供大家参考。具体如下:

<?php 

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(&#39;1986-07-22&#39;); 
?>
Copy after login

再来个简单点的,感觉没有上面的高大上

<?php
echo birthday("1989-01-25");
function birthday2($birthday){
 list($year,$month,$day) = explode("-",$birthday);
 $year_diff = date("Y") - $year;
 $month_diff = date("m") - $month;
 $day_diff  = date("d") - $day;
 if ($day_diff < 0 || $month_diff < 0)
  $year_diff--;
 return $year_diff;
}
Copy after login

希望本文所述对大家的php程序设计有所帮助。

相关文章:

PHP根据生日计算年龄(周岁)

php根据生日计算年龄/生肖/星座实例

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template