How to Calculate an Age By Birthday Using PHP

PHP Code Snippet

DISCLAIMER: This post may contain “affiliate” links to products and services I recommend. I’ll receive a small commission if you decide to purchase one of these products or services. I only recommend products I genuinely believe will help you in running your freelance business.

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

You might also like

Do you want more freelance clients?

I’ll show you what I learned over the last 15 years to grind out (from absolute scratch) a backlog of new clients wanting to hire you. Who your best client prospect are, what services you should be offering them, where to find them and more. Just enter your email address in the box below and let’s get started:

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on pinterest
John Morris


I’m a 15-year veteran of freelance web development. I’ve worked with bestselling authors and average Joe’s next door. These days, I focus on helping other freelancers build their freelance business and their lifestyles.

This Post Has 13 Comments

  1. Why not just do this?

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

  2. 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 🙂


      1. 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?

        1. 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.

          1. 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); ?>

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

          3. 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().

          4. 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!!

  3. Very smart way to do the job, congrats.

Leave a Reply

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

Do you want more freelance clients?

Enter your email below to get started building your system for consistently bringing in new freelance clients:


Bob Patterson

Not only is John a very talented programmer and developer, he is also an excellent communicator. He has a talent for taking complex subjects and communicating them in terms that anyone can understand. This is a rare combination. This ability has enabled me to take my skills and knowledge to the next level. Thank you John for for all that you do.

Daniel Mohlendick

On the Freelancing on Upwork course: “This is by far the best course i have watched on Skillshare!! Thank you so much.”

Ray Edwards

I recommend John every chance I get. If every person I worked with were as committed to excellence, punctuality, value, and unquestionable integrity… the world would be a better place. Highest recommendation.

Bradley Smith

John and I have worked together on numerous projects. John is very quick and efficient and was a pleasure to work with.

Xan Barksdale

Xan Barksdale

Very professional worker who is extremely knowledgable in WordPress and Wishlist Member. I would definitely hire again.

Lori Grant

John did an outstanding job on my project. I highly recommend him and look forward to working with him on future projects.

Lewis Howes

John is amazing at building membership sites. He converted one of my sites over from it’s existing (hardly working) platform over to the clean and simple to use WishList membership platform. I highly recommend using John and WishList for any of your membership site needs.

Close Menu