2013-10-22 324 views
53

我在sql中有一個用戶表,他們每個都有出生日期。我想將其出生日期轉換爲他們的年齡(僅限於年齡),例如日期:15.03.1999年齡:14和15.03.2014將變爲年齡:15根據出生日期計算年齡

在這裏,我想向用戶顯示的日期:

if(isset($_GET['id'])) 
{ 
    $id = intval($_GET['id']); 
    $dnn = mysql_fetch_array($dn); 
    $dn = mysql_query('select username, email, skype, avatar, ' . 
     'date, signup_date, gender from users where id="'.$id.'"'); 
    $dnn = mysql_fetch_array($dn); 
    echo "{$dnn['date']}"; 
} 
+0

可能重複[在MySQL計算時代(InnoDB的)](http://stackoverflow.com/questions/5773405/calculate -age-mysql-innodb) –

+0

想必您的日期是使用日期數據類型存儲的嗎? – Strawberry

+2

有很多'計算年齡'的答案。 Google是一個強大的工具!但順便說一下:不要使用mysql_ *函數。使用PDO或MySQLi –

回答

161

PHP> = 5.3.0

# object oriented 
$from = new DateTime('1970-02-01'); 
$to = new DateTime('today'); 
echo $from->diff($to)->y; 

# procedural 
echo date_diff(date_create('1970-02-01'), date_create('today'))->y; 

demo

功能:date_create()date_diff()


的MySQL> = 5.0.0

SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age 

demo

功能:TIMESTAMPDIFF()CURDATE()

+0

致命錯誤:調用未定義的函數date_diff() – PHPupil

+0

@PHPupil:'date_diff'用於PHP版本> = 5.3.0。 –

+0

@PHPupil:升級PHP? ;)或使用MySQL解決方案。 –

-2

的生日日期格式日/月/年

function age($birthday){ 
list($day, $month, $year) = explode("/", $birthday); 
$year_diff = date("Y") - $year; 
$month_diff = date("m") - $month; 
$day_diff = date("d") - $day; 
if ($day_diff < 0 && $month_diff==0) $year_diff--; 
if ($day_diff < 0 && $month_diff < 0) $year_diff--; 
return $year_diff; 
} 

或接受日,月,年爲參數相同的功能:

function age($day, $month, $year){ 
$year_diff = date("Y") - $year; 
$month_diff = date("m") - $month; 
$day_diff = date("d") - $day; 
if ($day_diff < 0 && $month_diff==0) $year_diff--; 
if ($day_diff < 0 && $month_diff < 0) $year_diff--; 
return $year_diff; 
} 

您可以使用它像這樣:

echo age("20/01/2000"); 

將輸出正確的年齡(6月4日,這是14)。

0

聲明@dateOfBirth date select @dateOfBirth = '2000-01-01'

SELECT datediff(YEAR,@dateOfBirth,getdate()) as Age

-1
$dob = $this->dateOfBirth; //Datetime 
     $currentDate = new \DateTime(); 
     $dateDiff = $dob->diff($currentDate); 
     $years = $dateDiff->y; 
     $months = $dateDiff->m; 
     $days = $dateDiff->d; 
     $age = $years .' Year(s)'; 

     if($years === 0) { 
      $age = $months .' Month(s)'; 
      if($months === 0) { 
       $age = $days .' Day(s)'; 
      } 
     } 
     return $age; 
4

得到了淨這個腳本(感謝coffeecupweb)

<?php 
/** 
* Simple PHP age Calculator 
* 
* Calculate and returns age based on the date provided by the user. 
* @param date of birth('Format:yyyy-mm-dd'). 
* @return age based on date of birth 
*/ 
function ageCalculator($dob){ 
    if(!empty($dob)){ 
     $birthdate = new DateTime($dob); 
     $today = new DateTime('today'); 
     $age = $birthdate->diff($today)->y; 
     return $age; 
    }else{ 
     return 0; 
    } 
} 
$dob = '1992-03-18'; 
echo ageCalculator($dob); 
?> 
+0

對我而言感謝 –

10

非常小的代碼來獲得年齡:

<?php 
    $dob='1981-10-07'; 
    $diff = (date('Y') - date('Y',strtotime($dob))); 
    echo $diff; 
?> 

//output 35 
+2

這是怎麼得到11票?一個人的年齡也取決於一年中的一天和一月。 –

0

參考鏈接http://www.calculator.net/age-calculator.html

$hours_in_day = 24; 
$minutes_in_hour= 60; 
$seconds_in_mins= 60; 

$birth_date  = new DateTime("1988-07-31T00:00:00"); 
$current_date = new DateTime(); 

$diff   = $birth_date->diff($current_date); 

echo $years  = $diff->y . " years " . $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>"; 
echo $months = ($diff->y * 12) + $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>"; 
echo $weeks  = floor($diff->days/7) . " weeks " . $diff->d%7 . " day(s)"; echo "<br/>"; 
echo $days  = $diff->days . " days"; echo "<br/>"; 
echo $hours  = $diff->h + ($diff->days * $hours_in_day) . " hours"; echo "<br/>"; 
echo $mins  = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour) . " minutest"; echo "<br/>"; 
echo $seconds = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour * $seconds_in_mins) . " seconds"; echo "<br/>"; 
1

的別名PHP的DateTime類可以給你幾年的年齡。假設$dobyyyy-mm-dd格式,此示例忽略一天中的時間。

date_diff(date_create($dob), date_create(date('Y-m-d'))->y 
0

在下面的代碼,date()date_create()date_diff()函數用來計算用戶到今天的時代。

$dateOfBirth = "17-10-1985"; 
$today = date("Y-m-d"); 
$diff = date_diff(date_create($dateOfBirth), date_create($today)); 
echo 'Age is '.$diff->format('%y'); 
0

有使用PHP

$birth_date = '15.03.2014'; 
$date = substr($birth_date, 0, 2); 
echo $date; 

的SUBSTR這將只是簡單的給你出生日期的輸出日期找到任何的出生日期的簡單方法。

在這種情況下,那將是

substr of PHP更多...

0
<?php 
function getAge($dob) { 
$today = date("Y-m-d"); 
$diff = date_diff(date_create($dob), date_create($today)); 
return $diff->format('%yYears, %mMonths, %dDays'); 
} 

echo getAge('19-10-1988'); 
?> 

Demo-1 Demo-2

相關問題