Jump to content

Finding Age from Birthdate


evil_stevo

Recommended Posts

//calculate age
$birthdate = "1978-04-26"; //birth date... actually being obtained from a database
$today = date("Y-m-d H:i:s"); // The exact date
$age = date_diff($str_birthday, $today);

echo $age;

 

I'd like a simple code to echo the age of someone with the mysql database information that's in their record. This doesn't work. I have no idea why. Nothing seems to work that I've found on the net. Please help. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/221776-finding-age-from-birthdate/
Share on other sites

What do you mean "doesn't work"? Are any errors displayed, or logged? What version of PHP are you using? Does date_diff() work for you in other scripts?

 

Finally, and importantly, have you read the manual page for date_diff()?

Perhaps...

 

<?php
$dob = "1978-04-26";
$ageTime = strtotime($dob); // Get the person's birthday timestamp
$t = time(); // Store current time for consistency
$age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;$year = 60 * 60 * 24 * 365;
$ageYears = $age / $year;
?>

That worked great litebearer!!! I did a little modification for my own and got rid of the decimal and this came out...

 

$birthdate = "1985-07-15";

//calculate age
$ageTime = strtotime($birthdate); // Birthday Timestamp
$t = time(); // Current Time
$age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;$year = 60 * 60 * 24 * 365;
$ageYears = $age / $year;
$age = number_format($ageYears,0); //Delete Decimals

echo $age;

 

Let me know what you think... THANKS AGAIN! IT WAS JUST THE TICKET!!!! :) :) :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.