【发布时间】:2013-09-06 14:53:29
【问题描述】:
我无法让我的脚本计算年龄,我从数据库中提取了生日,我希望脚本将该日期识别为生日。它从我的数据库中出来的格式是年/月/日
echo "Player Name: ".$row['FirstName']." ".$row['LastName']."<br>";
echo "Position: ".$row['Position']."<br>";
echo "Height: ".$row['Height']."<br>";
echo "Weight: ".$row['Weight']."<br>";
echo "Birthdate: ".$row['DOB']."<br>";
echo //date in yyyy.mm.dd format; or it can be in other formats as well
$DOB = "2001.12.31";
//explode the date to get year, month and day
$DOB = explode(".", $DOB);
//get age from date or DOB
$age = (date("md", date("U", mktime(0, 0, 0, $DOB[0], $DOB[1], $DOB[2]))) > date("md") ? ((date("Y")-$DOB[2])-1):(date("Y")-$DOB[2]));
echo "Age is:".$age."<br>";
echo "CNGHL Team: ".$row['CNGHLRights']."<br>";
echo "NHL Team: ".$row['Team']."<br>";
echo "Draft Year: ".$row['CNDraftYR']."<br>";
echo "Draft Position: ".$row['CNDraftPOS']."<br>";
echo "Drafted By: ".$row['CNDraftTEAM']."<br>";
echo "<img src=\"http://www.cnghl.info/cnghldb/images/".$iPlayerID.".jpg\">";
它从脚本 2001.12.31 中的日期中提取,而不是脚本从带有 DOB 的数据库中提取的日期。
任何帮助将不胜感激。
谢谢!
【问题讨论】:
-
1) 日期字段应该是DATE类型的,为什么这样存储?它是一个 VARCHAR 吗? 2) 看看 DateTime 类:php.net/manual/en/book.datetime.php