PHP 年齡計算函數(shù)(精確到天)
更新時間:2012年06月07日 16:45:11 作者:
因為一個具體的項目中要用到年齡計算,根據(jù)生日計算出當前的年齡。這個精確到天,和騰訊是一樣的,大家有需要的可以參考一下
復制代碼 代碼如下:
<?php
/**
* PHP 年齡計算函數(shù)
*
* 參數(shù)支持數(shù)組傳參和標準的 Mysql date 類型傳參
* params sample
* --------------------------------------------------
$birthArr = array(
'year' => '2000',
'month' => '11',
'day' => '3'
);
$birthStr = '2000-11-03';
* --------------------------------------------------
* );
* @author IT不倒翁 <itbudaoweng@gmail.com>
* @copyright (c) 2011,2012 Just Use It!
* @link IT不倒翁 http://yungbo.com
* @param string|array $birthday
* @return number $age
*/
function getAge($birthday) {
$age = 0;
$year = $month = $day = 0;
if (is_array($birthday)) {
extract($birthday);
} else {
if (strpos($birthday, '-') !== false) {
list($year, $month, $day) = explode('-', $birthday);
$day = substr($day, 0, 2); //get the first two chars in case of '2000-11-03 12:12:00'
}
}
$age = date('Y') - $year;
if (date('m') < $month || (date('m') == $month && date('d') < $day)) $age--;
return $age;
}
相關文章
微信公眾平臺開發(fā)教程③ PHP實現(xiàn)微信公眾號支付功能圖文詳解
這篇文章主要介紹了微信公眾平臺開發(fā)PHP實現(xiàn)微信公眾號支付功能,結合圖文形式詳細分析了基于php的微信公眾號支付功能開發(fā)流程、原理及相關操作技巧,需要的朋友可以參考下2019-04-04php array_key_exists() 與 isset() 的區(qū)別
這篇文章主要介紹了php array_key_exists() 與 isset() 的區(qū)別的相關資料,需要的朋友可以參考下2016-10-10