根据出生date计算年龄

我有一个用户在SQL表,他们每个人都有出生date。 我想把他们的出生date转换成他们的年龄(仅年数),例如:date: 15.03.1999年龄:14岁和15.03.2014将会变成年龄: 15

在这里我想显示用户的date:

 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']}"; } 

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; 

演示

函数: date_create()date_diff()


MySQL > = 5.0.0

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

演示

函数: TIMESTAMPDIFF()CURDATE()

非常小的代码得到年龄:

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

从网上得到这个脚本(感谢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); ?> 

PHP的DateTime类的别名可以给你几年的年龄。 假设$dobyyyy-mm-dd格式,此示例忽略一天中的时间。

 date_diff(date_create($dob), date_create(date('Ym-d'))->y 

参考链接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/>"; 

在以下代码中, date()date_create()date_diff()函数用于计算用户的年龄,直到今天。

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

有一个简单的方法,通过使用PHP的substr从任何出生datefinddate

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

这只是简单地给你出生date的输出date。

在这种情况下,这将是15

有关更多信息,请参阅PHP的substr …

 <?php function getAge($dob) { $today = date("Ymd"); $diff = date_diff(date_create($dob), date_create($today)); return $diff->format('%yYears, %mMonths, %dDays'); } echo getAge('19-10-1988'); ?> 

Demo-1 Demo-2

declare @dateOfBirth date select @dateOfBirth = '2000-01-01'

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

  $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; 

对于格式为date/月/年的生日date

 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)。