用php修改日期

时间:2011-11-09 17:20:47

标签: php mysql

基本上这是我用来从php中提取日期并修改它的函数:

      function getAge()
  {
         $result= mysql_query("SELECT date_of_birth 
                        FROM controlpanel 
                        WHERE user_id=".$this->userID) or die(mysql_error());
         $year="";
          if($row=mysql_fetch_array($result))
          {
               $row['date_of_birth'];
          }


  }

$行[ 'DATE_OF_BIRTH'];我想从那一行得到年份,并检查它是否是0000 ..如果它是我想要返回“未给出”; ...如果它不是,我想比较完整日期dmy到今天日期..为了得到人的出生日期..但是,我不确定使用什么日期功能..

4 个答案:

答案 0 :(得分:0)

$year = date("Y", $row['date_of_birth']);

if ($year == 0000) {
    return "Not given";
}

return date("d m Y", $row['date_of_birth']);

答案 1 :(得分:0)

if( substr($row['date_of_birth'],0,4) == "0000") echo "Not given";

答案 2 :(得分:0)

这将为您提供结果中的4位数年份:     $ year = date('Y',strtotime($ row ['date_of_birth']);

然后,你可以使用类似下一页的功能来获得年龄:

http://www.geekpedia.com/code79_Calculate-age-from-birth-date.html

或其他解决方案: http://www.google.com/search?gcx=w&sourceid=chrome&ie=UTF-8&q=php+get+age+from+birthday

答案 3 :(得分:0)

需要添加一些引号来声明0000作为字符串原因如果你使用0000而不是“0000”php会把它读成0

$year = date("Y", $row['date_of_birth']);
if ($year == "0000") {
    return "Invalid year";
}
else{
    return "Valid year";
}
相关问题