2012-11-05 111 views
-2

可能重複:
How to calculate age (in years) based on Date of Birth and getDate()從出生日期得到年齡?

我想呼應從他們存儲在MySQL表的出生日期一個人的年齡。有沒有我可以放入的一段代碼?

目前它只是顯示出生日期,因爲這就是我要求它做的所有事情,但如果這是可能的,我想要一種顯示年齡的方法。

下面是users.php while循環:

<? 

      $platinum_set = get_platinum_user(); 
      while ($platinum = mysql_fetch_array($platinum_set)) { 
       echo" 
       <div class=\"platinumcase\"> 
       <a href=\"profile.php?id={$platinum['id']}\"><img width=80px height= 80px src=\"data/photos/{$platinum['id']}/_default.jpg\" class=\"boxgrid\"/></a><h58> {$platinum['first_name']} {$platinum['last_name']}</h58><br/><br/> 

    <h52>{$platinum['dob']}<br/> 


<br/>{$platinum['location']}</h52> 
        </div>"; 
       } 
      ?> 

這裏的功能:

function get_platinum_user() { 
      global $connection; 
      $query = "SELECT * 
         FROM ptb_users, ptb_profiles 
         WHERE ptb_users.account_type = \"User\" 
         AND ptb_users.account_status = \"Active\" 
         AND ptb_profiles.user_id = ptb_users.id 
         AND ptb_users.subscription = \"Platinum\" 
         LIMIT 0 , 30"; 
      $platinum_set = mysql_query($query, $connection); 
      confirm_query($platinum_set); 
      return $platinum_set; 
     } 
+1

因此計算年齡。你有什麼嘗試? –

+0

我試過這個while循環,但它不起作用,仍然顯示出生日期: –

+0

$ dob = $ platinum ['dob']; function age_from_dob($ dob){ list($ y,$ m,$ d)= explode(' - ',$ dob); (($ m =(date('m') - $ m))<0){ $ y ++; else if($ m == 0 && date('d') - $ d <0){ $ y ++; } 返回日期('Y') - $ y; } $ dob = age_from_dob($ dob); ?> –

回答

0

您可以使用類似

(time() - strtotime($platinum['dob'])) to get the number of seconds 

一旦有了這個值,轉換它變成基於哪一年是閏年等的年份等。

從來自#1 Seconds to Year這個例子

可以使它計算的年

$x = strtotime($platinum['dob'])); 

$sy = 31536000 // seconds a non leap year 
$sb = 126230400 // seconds a block of 3 non leap years and one that is 

$actual_year = (floor((($x + $sy)/$sb)) + 492) * 4 + 
       (min(floor((($x + $sy) % $sb)/$sy) + 1, 4)); 
0

讓對方的DOB進行D1/M1/Y1, 數和今天的日期是D2的功能/ 2/Y 2,

現在

$y = $y2 - $y1; 
$m = $m2 - $m1; 
$d = $d2 - $d1; 

if($m>0) {$age=$y} 

else if($m<0) {$age=$y-1} 

else if($d>0) {$age=$y} 

else if($d<0) {$age=$y-1} 
else{echo 'happy birthday!'} 
+0

-1。如果有更簡單的方法可以使用,那將會非常痛苦。 –

+0

但這個算法仍然用於@marc b的目的 –

0
SELECT DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(dob, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(dob, '00-%m-%d')) AS age 

自年代開始工作。您的Mysql版本可能會受到支持 - 您是否使用Mysql?

然後,你還可以創建一個函數,I found it on the manual page of Mysql Date and Time functions的地方,日期和時間相關的東西訪問你的數據庫:

CREATE FUNCTION age (_d DATETIME) RETURNS INTEGER 
COMMENT 'Given birthdate, returns current age' 
RETURN YEAR(NOW()) - YEAR(_d) - IF(DATE_FORMAT(_d, '%c%d') > DATE_FORMAT(NOW(), '%c%d'), 1, 0); 

它首先substracts現在的日期,然後將它的年檢查一個月中的哪一天是現在之前還是之後,並相應地處理它。我會說這很簡單。自然而然,你在PHP中使用相同的邏輯來做到這一點。

如果沒有存儲dob的日期時間,幫助自己把它轉換成datetime它傳遞給函數之前:

SELECT *, age(dob) AS age FROM .... 

,你會發現它的名字age下的結果集裏面,然後,像$platinum['age']如果你有這個問題。

0
SELECT YEAR(now()) - YEAR(birth_date) - (DAYOFYEAR(now()) <= DAYOFYEAR(birth_date)) AS age 

例如

birth_day = 2005-04-03 (bday already occured this year) 
now = 2012-11-05 

    2012 - 2005 - (310 <= 93) 
    7 - (false aka 0) 
    7 

birth_day = 2005-11-06 (bday hasn't occured yet this year) 
now = 2012-11-05 

    2012 - 2005 - (309 <= 310) 
    7 - (true aka 1) 
    6 
相關問題