douduan6731 2012-07-03 06:44 采纳率: 0%
浏览 27
已采纳

需要在引号内输入日期

I'm trying to pass a date to a function that calculates the age of a person. However, the date is in Y-m-d format in the database, and I need to pass it in "Y-m-d" format. I have tried string concatenation, but that failed, maybe because it just operated the numbers using minus(-) operator. Kindly let me know how to do the same.

I am fetching the DOB in $dob variable, and passing it to CalculateAge($dateofbirth) function

Here is the code:

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条回答 默认 最新

  • du22399 2012-07-03 06:54
    关注

    This would probably work -

    $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); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?