Support

Account

Home Forums General Issues Calculate Age from Date field

Solved

Calculate Age from Date field

  • I have a client for whom I am using ACF to populate custom posts about kids up for adoption. Right now I have them entering the kids’ ages in a field, but they are asking me if it is possible for them to enter a birthdate and have it calculate and show the age. I … have no idea how or if this is possible, but I told them I would ask!

    Anyone know of a way to do this?

  • What is the date format being used for this field? Some PHP should be able to handle this based on the current date. Do you have a QA site you can share with me?

  • <?php 
        $date = get_field('date_of_birth');
        $birthday = new DateTime($date);
        $interval = $birthday->diff(new DateTime);
        echo $interval->y.' years old';
    ?>
  • This worked for me on my test: http://devwrangler.com/dob-test/

    <?php
      //date in mm/dd/yyyy format; or it can be in other formats as well
      $birthDate = get_field('date_of_birth');
      //explode the date to get month, day and year
      $birthDate = explode("/", $birthDate);
      //get age from date or birthdate
      $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
        ? ((date("Y") - $birthDate[2]))
        : (date("Y") - $birthDate[2]));
      echo "Age:" . $age;
    ?>
  • Thanks, I will give these a try! I was going to use a date picker, in answer to the first question.

  • This one worked for me!

    
    <?php 
        $date = get_field('date_of_birth');
        $birthday = new DateTime($date);
        $interval = $birthday->diff(new DateTime);
        echo $interval->y.' years old';
    ?>
    

    This one:

    
    <?php
      //date in mm/dd/yyyy format; or it can be in other formats as well
      $birthDate = get_field('date_of_birth');
      //explode the date to get month, day and year
      $birthDate = explode("/", $birthDate);
      //get age from date or birthdate
      $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
        ? ((date("Y") - $birthDate[2]))
        : (date("Y") - $birthDate[2]));
      echo "Age:" . $age;
    ?>
    

    For some reason generated the current year. But no matter, I have it all working now, thanks!

  • Yay! Good luck and glad it worked.

    Also, I checked out your site (Googled “websydaisy”). Nice work.

  • This reply has been marked as private.
  • Hi,

    I have been trying to calculate a member of staff’s years of service and have had some success following this guide 🙂

    This is working great, however, I would really like to display the result as “14th Year of Service” or “3rd Year of Service” and can’t figure it out could you offer some guidance please? I have been looking into it and it looks like oridnal suffix is what I am looking for.

    My PHP code is:

    <p class=”start-date”> <?php if( get_sub_field(‘startdate’) ): ?> <?php $date = get_sub_field(‘startdate’); $yearsofservice = new DateTime($date); $interval = $yearsofservice->diff(new DateTime); echo $interval->y. ‘ Year of Service’; ?></p> <?php endif; ?>

    Thank you in advance

    Darren

  • Hi,

    I have been trying to calculate a member of staff’s years of service and have had some success following this guide 🙂

    This is working great, however, I would really like to display the result as “14th Year of Service” or “3rd Year of Service” and can’t figure it out could you offer some guidance please? I have been looking into it and it looks like oridnal suffix is what I am looking for.

    My PHP code is:

    <p class=”start-date”> <?php if( get_sub_field(‘startdate’) ): ?> <?php $date = get_sub_field(‘startdate’); $yearsofservice = new DateTime($date); $interval = $yearsofservice->diff(new DateTime); echo $interval->y. ‘ Year of Service’; ?></p> <?php endif; ?>

    Thank you in advance

    Darren

Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘Calculate Age from Date field’ is closed to new replies.