Close

December 8, 2013

How to Calculate an Age By Birthday Using PHP

This PHP function accepts a birthday as a parameter and will return the age based on that birthday. Handy for social sites/applications.

13 Comments on “How to Calculate an Age By Birthday Using PHP

Kisuka
December 12, 2013 at 6:41 pm

Why not just do this?

echo date_create(‘1981-05-18’)->diff(date_create(‘today’))->y;

Reply
John Morris
December 13, 2013 at 4:34 pm

Updated. Thanks!

Reply
horechekArtem
December 12, 2013 at 11:08 pm

diff($now);
echo $diff->format(‘%y’);

Reply
Sarah
August 9, 2014 at 4:51 pm

Hi John, will you please help me with something? I’m a custom field in wordpress where the user enters a date from a date picker. How do I grab that custom field and run this function on it? Thanks 🙂

-Sarah

Reply
John Morris
August 9, 2014 at 6:27 pm

You’d use get_user_meta() to grab it from WordPress: http://codex.wordpress.org/Function_Reference/get_user_meta

…then drop it into the function as needed.

Reply
Sarah
August 9, 2014 at 6:32 pm

Thanks for the quick reply!

I’m actually doing a custom post type for a client for their testimonials with two fields- the testimonial itself and a date picker so they can manually input the dob. This would change the way I can display the data, then… any ideas?

Reply
John Morris
August 9, 2014 at 6:46 pm

Shouldn’t change anything. I’m assuming you want to return the person’s age from birthday. So in your template file where you’re displaying the testimonial, it’d be something like:

$birthday = get_user_meta($post->ID, $custom_field_name, true);
$age = birthday($birthday);

echo $age;

If I understanding what you’re saying… something like that should work.

Reply
Sarah
August 10, 2014 at 12:10 pm

function birthday($birthday){

$age = strtotime($birthday);

if($age === false){
return false;
}

list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age));

$now = strtotime("now");

list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now));

$age = $y2 - $y1;

if((int)($m2.$d2) diff(date_create('today'))->y;

return $age;
}

echo birthday($testimonial_age); ?>

Sarah
August 10, 2014 at 12:27 pm

I’m using this Fatal error: Cannot redeclare birthday()

John Morris
August 10, 2014 at 1:14 pm

You don’t need to use BOTH functions in the source code I posted. It’s one or the other. If you’re PHP 5.3 or greater than use the one on bottom. Chance are, you are on PHP 5.3 so I’d try it first. If it doesn’t work then use the top one. The error is because you’re declaring the function twice by copying all the code.

Also, I’d recommend changing the function name to something besides just birthday() as that’s pretty generic. If this is in a plugin and you’re NOT using a class then you should be prefixing your function names. For example, my theme name is Evolution of Dance… so all the functions are prefixed with evd. So, I’d do evd_birthday().

Sarah
August 10, 2014 at 1:59 pm

function birthday_basc($birthday) {
$birthday = get_post_meta($post->ID, ‘testimonial_age’, true);
$age = $birthday;

if($age === false){
return false;
}

list($y1,$m1,$d1) = explode(“-“,date(“Y-m-d”,$age));

$now = strtotime(“now”);

list($y2,$m2,$d2) = explode(“-“,date(“Y-m-d”,$now));

$age = $y2 – $y1;

if((int)($m2.$d2) < (int)($m1.$d1))
$age -= 1;

return $age;
}

I made this a function in functions.php. Here's what I put in my template file:

php birthday_basc();

Nothing displays. I'm way out of my lane, here… obv. Thanks for your help!!

Sarah
August 10, 2014 at 12:19 pm
Michael Anderson
January 6, 2015 at 10:35 am

Very smart way to do the job, congrats.

Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top