Date

A friend of mine gave me this code to show a person’s age from their birth date and it works fine, except, when there is no birth date it shows Birthdate: (43). So long story short, does anyone know how I can get it to display Unknown if there is no date added for that person?

Here is the code I’m using:
// GET AGE FROM BIRTHDATE ($age)

$bdate = $row_getIdol["bd"];  // FROM SQL

if($bdate == 0){ $nodate = 1; }
$bdate = strtotime( $bdate );
$birthday = date("n/j/Y",$bdate);  //must be as m/d/yyyy

$bday = explode("/", $birthday); //parse
$b_mm = $bday[0]; //birthday month
$b_dd = $bday[1]; //birthday day
$b_yyyy = $bday[2]; //birthday year

//compare timestamps of mm/dd for birthday and today
$bday_mm_dd = mktime(0,0,0,$b_mm,$b_dd,0);
$today_mm_dd = mktime(0,0,0,date("m"),date("d"),0);
$age = date("Y", time()) - $b_yyyy;

if ($bday_mm_dd > $today_mm_dd) {
//birthday hasnt happened yet this year
$age = $age - 1;
} 

// END GET AGE FROM BIRTHDATE

Hello,

You can simply use the first IF statement to your advantage and if there is no birthdate returned from the database you simply ignore the rest of the code and set the $age variable (I believe that is being used for display) to UNKNOWN. Take a look at my script and see if it does what you want.

[php]// GET AGE FROM BIRTHDATE ($age)

$bdate = $row_getIdol[“bd”]; // FROM SQL

if(isset($bdate)){
$bdate = strtotime( $bdate );
$birthday = date(“n/j/Y”,$bdate); //must be as m/d/yyyy

$bday = explode("/", $birthday); //parse
$b_mm = $bday[0]; //birthday month
$b_dd = $bday[1]; //birthday day
$b_yyyy = $bday[2]; //birthday year

//compare timestamps of mm/dd for birthday and today
$bday_mm_dd = mktime(0,0,0,$b_mm,$b_dd,0);
$today_mm_dd = mktime(0,0,0,date(“m”),date(“d”),0);
$age = date(“Y”, time()) - $b_yyyy;

if ($bday_mm_dd > $today_mm_dd) {
//birthday hasnt happened yet this year
$age = $age - 1;
}
} else {
$age = ‘UNKNOWN’;
}

// END GET AGE FROM BIRTHDATE[/php]

This goes on the assumption that the $age variable is used to display the birthdate. If it’s not, you can substitute the $age variable with the variable that is being used to display UNKNOWN.

Cheers!

Sponsor our Newsletter | Privacy Policy | Terms of Service