0

我正在尝试将日期传递给计算一个人年龄的函数。但是,数据库中的日期是 Ymd 格式,我需要以“Ymd”格式传递它。我尝试过字符串连接,但失败了,可能是因为它只是使用减号(-)运算符来操作数字。请让我知道如何做同样的事情。

我在 $dob 变量中获取 DOB,并将其传递给 CalculateAge($dateofbirth) 函数

这是代码:

function CalculateAge($BirthDate)
{
        // Put the year, month and day in separate variables
        list($Year, $Month, $Day) = explode("-", $BirthDate);

    //echo $Year;       

        $YearDiff = date("Y") - $Year;
        // If the birthday hasn't arrived yet this year, the person is one year younger
        if(date("m") < $Month || (date("m") == $Month && date("d") < $Day))
        {
                $YearDiff--;
        }
    if(date("m") > $Month || date("m") == $Month)
        $MonthDiff = date("m") - $Month;
    else
        $MonthDiff = 12 - $Month + date("m");

    $age = $YearDiff + $MonthDiff/12;
        return $age;
}
$dob = mysql_query("SELECT date_of_birth FROM kids_informations WHERE user_id = '$usid'");  
$rs = CalculateAge($dob);   
4

2 回答 2

0

用这个:echo date("\"Y-m-d\"");

于 2012-07-03T06:47:36.863 回答
0

这可能会奏效-

$result = mysql_query("SELECT date_of_birth FROM kids_informations WHERE user_id = '$usid'");  

while($row = mysql_fetch_array($result))
{
   $dob = $row['date_of_birth'];
}

$dob = "\"".$dob."\"";

$rs = CalculateAge($dob); 
于 2012-07-03T06:54:25.710 回答