2011-01-27 118 views
0

作爲我想要做什麼的簡短說明: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);} 

感謝提前任何幫助。

回答

2

請記住,$ row是一個關聯數組而不是一個字符串變量,因此您需要做$ row ['birthday'],否則您將傳入一個數組對象到函數中。我在下面的代碼中解釋過。

希望它可以幫助

// 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 

$birthdays = mysql_query("SELECT birthday FROM user_records"); 

$birthdaysArray = array(); 

while($row=mysql_fetch_assoc($birthdays)) { $birthdaysArray[] = $row['birthday']; } 

//LOOP HERE TO FIND AGES 
0

那麼接下來,你需要遍歷您的陣列和使用功能轉換爲年齡。

$calculated_ages = array(); 
foreach($agearray as $d) 
{ 
    $calculated_ages[] = CalculateAge($d); 
} 

然後得到平均值。

+0

真棒傢伙,謝謝,我知道這一切工作!非常感謝。 – Kieran 2011-01-27 21:46:56

0

而不是拉入PHP,你可以從MySQL查詢直接做到這一點,

例如,下面的查詢返回一個用戶的天數平均年齡爲當前日期:

SELECT AVG(DATEDIFF(NOW(), birthday)) AS AverageAge FROM `user_records`; 
2
drop table if exists users; 

create table users 
(
user_id int unsigned not null auto_increment primary key, 
username varchar(32) unique not null, 
dob date not null 
) 
engine=innodb; 

insert into users (username, dob) values 
('f00','1960-01-01'), ('bar','1970-01-01'), 
('alpha','1980-01-01'), ('beta','1990-01-01'), 
('delta','2000-01-01'), ('theta','2010-01-01'), 
('zeta',curdate()); 

select 
if(sum(a.age) is null, 0, sum(a.age)) as sum_age, 
count(*) as count_ages, 
if(count(*) = 0, 0, sum(a.age)/count(*)) as avg_age 
from 
(
select 
date_format(now(), '%Y') - date_format(dob, '%Y') - (date_format(now(), '00-%m-%d') < date_format(dob, '00-%m-%d')) as age 
from users 
) a 

+---------+------------+---------+ 
| sum_age | count_ages | avg_age | 
+---------+------------+---------+ 
|  156 |   7 | 22.2857 | 
+---------+------------+---------+ 
1 row in set (0.00 sec) 
+0

這是一個很好的查詢:) – 2011-01-27 23:08:58

相關問題