Support » Plugin: Advanced Custom Fields » How to calculate age from a year of birth?

  • How could I calculate an person’s age if I have their year of birth?

    Thanks heaps
    Tim

    ——————————————
    EDIT:

    Im trying this function:

    function age_in_years() {
    	global $postmeta;
    	$year_of_birth = get_field( "date_of_birth",$modelID);
    	$date1 = strval( $year_of_birth );
    	$date2 = strval( date( 'yy') );
    	$age = $date2 = $date1;
    	return $age;
    }

    But is not working at all… what I am doing wrong?
    Using this code:
    <?php echo age_in_years();?>

    Is getting this:
    05/07/1992

    Database screenshot: http://i.imgur.com/mjLrEQx.png

    https://wordpress.org/plugins/advanced-custom-fields/~

    Waiting for your help, thanks 🙂

Viewing 1 replies (of 1 total)
  • Matthew Ray

    (@rayman813)

    I don’t think this is a question for the ACF support forum. It seems that ACF is working properly for you. These kind of questions should be asked on general support forums like StackOverflow. Here is the answer to your question, though:

    function age_in_years() {
        $year_of_birth = get_field( 'date_of_birth', $modelID );
        return intval( date( 'Y', time() - strtotime( $year_of_birth ) ) ) - 1970;
    }
Viewing 1 replies (of 1 total)
  • The topic ‘How to calculate age from a year of birth?’ is closed to new replies.