我正在建造一个小型网站并陷入困境。 你如何使用他们的出生日期来获得一个人的年龄?
所以这应该是这样的:
$age=curr_date - YEAR($birth_date)
我希望这个问题很清楚。
提前致谢
答案 0 :(得分:4)
使用date
和strtotime
来确定出生日期的人的年龄。
$birth_date = '1991-12-10';
echo $age= date("Y") - date("Y", strtotime($birth_date)); //25
答案 1 :(得分:1)
以下是代码:
$dob = new DateTime('2015-10-02');
$today = new DateTime;
$age = $today->diff($dob);
你可以像这样回应年龄:
echo $age->format('%y Years, %m Months and %d Days');
答案 2 :(得分:0)
首先在strtotime中转换你的日期
$userDob = '18/01/2000'; //Try to convert your date format like this
$userDob = strtotime($userDob);
$currDate = time();
$dateDiff = $currDate - $userDob;
echo Date('y',$dateDiff);
答案 3 :(得分:0)
假设$birth_date
格式为YYYY-MM-DD
。
您可以通过以下方式完成此操作。首先创建BirthDate的 DateTime 对象,
$date=date("Y-m-d",strtotime($birth_date));
$bDObj=new DateTime($date);
$cDate=new DateTime();
$age=$cDate->format("Y")-$bDObj->format("Y");
答案 4 :(得分:0)
您可以使用:
<?php
$from = new DateTime('1993-09-19');
$to = new DateTime('today');
echo $from->diff($to)->y;
?>
答案 5 :(得分:0)
<?php
$date1=date_create("2013-01-01");
$date2=date_create("2013-02-10");
$diff=date_diff($date1,$date2);
echo $diff->format("Total number of years: %y.");
?>
答案 6 :(得分:0)
<?php
// ASSUMING YOU HAVE THE DATE IN MYSQL FORMAT: Y-m-d
$birthDate = "1980-10-05";
$today = date("Y-m-d");
$dateA = new DateTime($today);
$dateB = new DateTime($birthDate);
$dateDiff = $dateA->diff($dateB);
$age = "{$dateDiff->y} Years, {$dateDiff->m} Months and {$dateDiff->d} Days";
var_dump($age); //<== DUMPS: '35 Years, 8 Months and 3 Days'