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

Complete HTML Tutorial for Beginners
Description Are you interested in starting a career in web development? This beginner-friendly HTML tutorial is the perfect starting point for you. In this comprehensive
This Post Has 13 Comments
Why not just do this?
echo date_create(‘1981-05-18’)->diff(date_create(‘today’))->y;
Updated. Thanks!
diff($now);
echo $diff->format(‘%y’);
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
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.
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?
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.
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); ?>
I’m using this Fatal error: Cannot redeclare birthday()
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().
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!!
Very smart way to do the job, congrats.