0

I am having trouble getting my script to calculate age, I have the birthday being pulled from a database and I want the script to recongize that date as the birthdate. The Format it comes out of my database is year/month/date I keep getting strange results with the script that I found on from looking up how to do this.

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\">";

It pulls from the date inside the script 2001.12.31 rather then the date that the script pulls from the database with the DOB.

Any help would be greatly appreciated.

Thanks!

4

4 回答 4

3

嗨,布莱恩·J·柯克。我认为这些链接可能会对您有所帮助。

php中的年龄计算

php中的当前年龄计算

于 2013-09-06T06:53:29.403 回答
2
 $date1 = "2012-09-13";
 $date2 = date("Y-m-d");
        $diff = abs(strtotime($date2) - strtotime($date1));
        $years = floor($diff / (365*60*60*24));
        $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
        $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
        $finaldate = ($years*365)+ ($months*30)+$days;
        $finalyear = floor($finaldate/365);
于 2013-09-06T05:21:23.463 回答
1
<?php
$DOB = "31.12.2001"; //dd.mm.yyyy
$user_date = new DateTime($DOB);
$curr_date = new DateTime(); 
$age_cal = $curr_date->diff($user_date);
echo $age_cal->y;
?>
于 2013-09-06T05:38:39.450 回答
0

用这个

<?php

     $birthDate = "2001.12.31";
     //explode the date to get month, day and year
     $birthDate = explode(".", $birthDate);
     //get age from date or birthdate
     $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[2], $birthDate[1],  $birthDate[0]))) > date("md") ? ((date("Y")-$birthDate[0])-1):(date("Y")-$birthDate[0]));
     echo "Age is:".$age;
于 2013-09-06T06:46:54.483 回答