How to calculate age from a year of birth?

Status
Not open for further replies.

Divvy

Active Member
806
2009
18
0
Hello guys,

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

Im trying this function:

PHP:
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:
<?php echo age_in_years();?>

Is getting this:
Code:
05/07/1992

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


Waiting for your help, thanks :)

Thanks heaps
Tim

__________________
Added after 22 Hours 54 minutes:

Ok, a new try... :)

If I have this code to calculate age:
PHP:
<?php echo date_diff(date_create('04-08-1967'), date_create('today'))->y; ?>

And If I have this one to get the birth date:
PHP:
<?php echo get_field( "date_of_birth",$modelID); ?>

I need to replace the '04-08-1967' of 1st code with the 2nd code right?
But how can I do that? My php knowledge is very poor...

I tried:
PHP:
<?php echo date_diff(date_create('get_field( "date_of_birth",$modelID);'), date_create('today'))->y; ?>

But without success... Im doing something wrong... help :)

Thanks!!
 
Last edited:
3 comments
This should accomplish what you are trying to do:

PHP:
<?php 
$age = get_field("date_of_birth",$modelID);

echo date_diff($age, date_create('today'))->y; 
?>
 
Last edited:
Status
Not open for further replies.
Back
Top