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.

Join 7,700 Other Freelancers Who've Built Thriving Freelance Businesses

Over 7,700 other freelancers have started thriving freelance businesses using the information on this blog. Are you next? Subscribe below to get notified whenever a new article is posted and create your own success story:

You might also like

Facebook
Twitter
LinkedIn
Reddit
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.

This Post Has 13 Comments

  1. Kisuka

    Why not just do this?

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

  2. horechekArtem

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

  3. Sarah

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

        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. John Morris

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

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

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

          3. John Morris

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

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

          5. Sarah
  4. Michael Anderson

    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.

Join 7,700 Other Freelancers Who've Built Thriving Freelance Businesses

Over 7,700 other freelancers have started thriving freelance businesses using the information on this blog. Are you next? Subscribe below to get notified whenever a new article is posted and create your own success story:

Success Stories

Ready to add your name here?

Tim Covello

Tim Covello

75 SEO and website clients now. My income went from sub zero to over 6K just last month. Tracking 10K for next month. Seriously, you changed my life.

Michael Phoenix

Michael Phoenix

By the way, just hit 95K for the year. I can’t thank you enough for everything you’ve taught me. You’ve changed my life. Thank you!

Stephanie Korski

Stephanie Korski

I started this 3 days ago, following John’s suggestions, and I gained the Upwork Rising Talent badge in less than 2 days. I have a call with my first potential client tomorrow. Thanks, John!

Jithin Veedu

Jithin Veedu

John is the man! I followed his steps and I am flooded with interviews in a week. I got into two Talent clouds. The very next day, I got an invitation from the talent specialists from Upwork and a lot more. I wanna shout out, he is the best in this. Thanks John for helping me out!

Divyendra Singh Jadoun

Divyendra Singh Jadoun

After viewing John’s course, I made an Upwork account and it got approved the same day. Amazingly, I got my first job the very same day, I couldn’t believe it, I thought maybe I got it by coincidence. Anyways I completed the job and received my first earnings. Then, after two days, I got another job and within a week I got 3 jobs and completed them successfully. All the things he says seem to be minute but have a very great impact on your freelancing career.

Sarah Mui

Sarah Mui

I’ve been in an existential crisis for the last week about what the heck I’m doing as a business owner. Even though I’ve been a business for about a year, I’m constantly trying to think of how to prune and refine services. This was very personable and enjoyable to watch. Usually, business courses like this are dry and hard to get through…. repeating the same things over and over again. This was a breath of fresh air. THANK YOU.

Waqas Abdul Majeed

Waqas Abdul Majeed

I’ve definitely learnt so much in 2.5 hours than I’d learn watching different videos online on Youtube and reading tons of articles on the web. John has a natural way of teaching, where he is passionately diving in the topics and he makes it very easy to grasp — someone who wants you to really start running your business well by learning about the right tools and implement them in your online business. I will definitely share with many of the people I know who have been struggling for so long, I did find my answers and I’m sure will do too.

Scott Plude

Scott Plude

I have been following John Morris for several years now. His instruction ranges from beginner to advanced, to CEO-level guidance. I have referred friends and clients to John, and have encouraged my own daughter to pay attention to what he says. All of his teachings create wealth for me (and happiness for my clients!) I can’t speak highly enough about John, his name is well known in my home.

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!

Mohamed Misrab

Misrab Mohamed

John has been the most important person in my freelance career ever since I started. Without him, I would have taken 10 or 20 years more to reach the position I am at now (Level 2 seller on Fiverr and Top Rated on Upwork).

Join 7,700 Other Freelancers Who've Built Thriving Freelance Businesses

Over 7,700 other freelancers have started thriving freelance businesses using the information on this blog. Are you next? Subscribe below to get notified whenever a new article is posted and create your own success story: