I have a custom field "DOB" on my custom post type which is in [Ymd] format. I want to compare that date of birth to current time and want to show "You are # years # months."
I am able to calculate Years only but it is not accurate. Help me.
You create two date objects and compare with the
$from = new DateTime( '1970-01-01' ); $to = new DateTime('today'); $years = $from->diff($to)->y; $months = $from->diff($to)->m; echo $years . ' years and ' . $months . ' months.';
=> 46 years and 1 months.