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:


Chris Aitken

He significantly improved my site through his expert knowledge of PHP, CSS and Javascript. Would definitely recommend John to others.

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.

Thabo Motsoahae

John is one of the best instructors I have come across, I learned a lot from his online tutorials.

Sukh Plaha

John is a fantastic and patient tutor, who is not just able to share knowledge and communicate it very effectively – but able to support one in applying it. However, I believe that John has a very rare ability to go further than just imparting knowledge and showing one how to apply it. He is able to innately provoke one’s curiosity when explaining and demonstrating concepts, to the extent that one can explore and unravel their own learning journey. Thanks very much John!

Daniel Mohlendick

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

Lori Grant

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

Xan Barksdale

Xan Barksdale

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

Close Menu