這篇文章主要介紹了php根據生日計算年齡的方法,涉及php操作日期與字符串的相關技巧,非常簡單實用,需要的朋友可以參考下
本文實例講述了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('1986-07-22');
- ?>
希望本文所述對大家的php程序設計有所幫助。
新聞熱點
疑難解答