需要在引号内输入日期

时间:2012-07-03 06:44:31

标签: php

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

我在$ 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);   

2 个答案:

答案 0 :(得分:0)

使用此:echo date("\"Y-m-d\"");

答案 1 :(得分: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);