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.

https://gist.github.com/7851237

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
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on reddit
Reddit
Share on pinterest
Pinterest
John Morris

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.

You might also like

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 🙂

    -Sarah

      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:

WHAT OTHERS ARE SAYING

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

Thabo Motsoahae

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

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.

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.

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