我在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']}";
}
Run Code Online (Sandbox Code Playgroud)
Gla*_*vić 179
# 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;
Run Code Online (Sandbox Code Playgroud)
SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age
Run Code Online (Sandbox Code Playgroud)
小智 9
获得年龄的代码非常小:
<?php
$dob='1981-10-07';
$diff = (date('Y') - date('Y',strtotime($dob)));
echo $diff;
?>
//output 35
Run Code Online (Sandbox Code Playgroud)
小智 5
从网络上获得了此脚本(感谢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);
?>
Run Code Online (Sandbox Code Playgroud)
$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 * $minutes_in_hour) + ($diff->days * $hours_in_day * $minutes_in_hour) . " minutest"; echo "<br/>";
echo $seconds = ($diff->h * $minutes_in_hour * $seconds_in_mins) + ($diff->days * $hours_in_day * $minutes_in_hour * $seconds_in_mins) . " seconds"; echo "<br/>";
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
150344 次 |
最近记录: |