Quantcast
Channel: Toolset » All Posts
Viewing all articles
Browse latest Browse all 20145

calculate+display current age based on date field

$
0
0

Hi, I want to calculate and display the current age of an item based on a date field. I've read http://wp-types.com/forums/topic/filter-between/page/2/ but to me that seems a bit of a workaround. I think this could be done easier:

There's a date value for this (custom, but that's beside the point) post type, and it's not the published date but an added field value. Now, the logic that needs to be added to the view is to determine the current date, calculate the difference (in years only), and display that value. The solution provided in the thread referenced above updates a field name when the admin logs in. But wouldn't adding the logic described to the view be easier? Something like this:

function age_in_years() {
	$year_of_birth = [the value of the custom field here];
	$date1 = strval( $year_of_birth );
	$date2 = strval( date( 'Y') );
	$age = $date2 - $date1;
	return $age;
}

How do I get the custom field value in this function, and anything I should not when adding this to the view code?


Viewing all articles
Browse latest Browse all 20145

Trending Articles