作爲我想要做什麼的簡短說明:PHP&MySql數組問題(非常討厭!)
我有一個MySql數據庫與各種列。其中一列是包含用戶出生日期的「生日」欄。
我想要做的是將本專欄中的所有出生日期,將其轉化爲一個年齡,然後計算出平均/平均年齡。
所以...我已經得到了兩部分工作的代碼,但我無法讓他們一起工作!
我可以提供生日功能我有一個出生日期,它會變成一個年齡。
我可以將所有的出生日期都存入一個數組中。
我不能做的是從這裏的任何事情。我無法把這個陣列變成時代,並且基本上找出了這個意思,這讓我感到非常緊張。我是PHP的新手,但是迄今爲止我已經做了相當多的工作。
真的很感謝能幫助你實現這個目標!我只是不知道從下面去哪裏。
這裏是我的代碼:
// Age calculation
function CalculateAge($BirthDate)
{
// Put the year, month and day in separate variables
list($Day, $Month, $Year) = explode("/", $BirthDate);
$YearDiff = date("Y") - $Year;
// If the birthday hasn't arrived yet this year, the person is one year younger
if(date("m") < $Month || (date("m") == $Month && date("d") < $DayDiff))
{
$YearDiff--;
}
return $YearDiff;
}
// How to use the function
// CalculateAge("24/06/1991");
//Birthdate array
$ages = mysql_query("SELECT birthday FROM user_records");
$agearray = array();
while($row=mysql_fetch_assoc($ages)) {array_push($agearray, $row);}
感謝提前任何幫助。
真棒傢伙,謝謝,我知道這一切工作!非常感謝。 – Kieran 2011-01-27 21:46:56