php 根据出生日期计算年龄
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19521146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate age based on date of birth
提问by PHPupil
I have a table of users in sql and they each have birth dates. I want to convert their date of birth to their age (years only), e.g. date: 15.03.1999
age: 14 and 15.03.2014
will change to age: 15
我在 sql 中有一个用户表,他们每个人都有出生日期。我想将他们的出生日期转换为他们的年龄(仅限年),例如 date: 15.03.1999
age: 1415.03.2014
并将更改为 age:15
Here I want to show the date of the user:
这里我想显示用户的日期:
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']}";
}
回答by Glavi?
PHP >= 5.3.0
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;
functions: date_create()
, date_diff()
MySQL >= 5.0.0
MySQL >= 5.0.0
SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age
functions: TIMESTAMPDIFF()
, CURDATE()
回答by easycodingclub
Very small code to get Age:
获取年龄的非常小的代码:
<?php
$dob='1981-10-07';
$diff = (date('Y') - date('Y',strtotime($dob)));
echo $diff;
?>
//output 35
回答by googli.us
Got this script from net (thanks to coffeecupweb)
从网上得到这个脚本(感谢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);
?>
回答by Shailesh Sonare
Reference Link http://www.calculator.net/age-calculator.html
参考链接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/>";
回答by Rutendo
$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;
回答by Subin
For a birthday date with format Date/Month/Year
对于格式为日期/月/年的生日日期
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;
}
or the same function that accepts day, month, year as parameters :
或接受日、月、年作为参数的相同函数:
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;
}
You can use it like this :
你可以这样使用它:
echo age("20/01/2000");
which will output the correct age (On 4 June, it's 14).
这将输出正确的年龄(6 月 4 日,它是 14)。
回答by Tayyab Hayat
$getyear = explode("-", $value['users_dob']);
$dob = date('Y') - $getyear[0];
$value['users_dob']
is the database value with format yyyy-mm-dd
$value['users_dob']
是具有格式的数据库值 yyyy-mm-dd
回答by Shaan Ansari
To Calculate age from Date of birth used used query like this.
从出生日期计算年龄使用这样的查询。
'SELECT username, email, skype, avatar, TIMESTAMPDIFF(YEAR, date, CURDATE()) AS age, signup_date, gender FROM users WHERE id="'.$id.'"';
if(isset($_GET['id']))
{
$id = intval($_GET['id']);
$dn = mysql_query('select username, email, skype, avatar, TIMESTAMPDIFF(YEAR, date, CURDATE()) AS age, signup_date, gender from users where id="'.$id.'"');
$dnn = mysql_fetch_array($dn);
echo $dnn['age'];
}
Note: don't use reserved keywords column name.
注意:不要使用保留关键字列名。
回答by Sushant Samleti
I hope you will find this useful.
我希望你会发现这很有用。
$query1="SELECT TIMESTAMPDIFF (YEAR, YOUR_DOB_COLUMN, CURDATE()) AS age FROM your_table WHERE id='$user_id'";
$res1=mysql_query($query1);
$row=mysql_fetch_array($res1);
echo $row['age'];
回答by YATHI
declare @dateOfBirth date
select @dateOfBirth = '2000-01-01'
宣布 @dateOfBirth date
select @dateOfBirth = '2000-01-01'
SELECT datediff(YEAR,@dateOfBirth,getdate()) as Age
SELECT datediff(YEAR,@dateOfBirth,getdate()) as Age