Jump to content

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!!!! :) :) :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.