Sorry, you want to display exact age with Year, months, days?
Yes I want to do that!
Minesh
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
Asia/Kolkata (GMT+05:30)
Hello. Thank you for contacting the Toolset support.
Well - To calculate the date difference in Years, Month and Days from current time, you can use the following shortcode:
add_shortcode( 'iam-eta', 'wpv_post_age_shortcode');
function wpv_post_age_shortcode( $atts ) {
global $post;
// gettitng time stamp
$bday = get_post_meta( $post->ID, 'wpcf-data-di-nascita', true );
$bday = new DateTime("@$ts");
$now = new DateTime('00:00:00'); // current date-time
// object with the difference between $now and $bday (y = year, m = month, d = day)
$diff = $now->diff($bday);
// output
return sprintf('%d years, %d month, %d days', $diff->y, $diff->m, $diff->d);
}
This code send the post page in crash, I display only the menu bars and all content desappear...
And sorry, I have don't understand correctly your first question. So, I want to display only the correct age, not month and day remaining.
So, I think that the correct solution is the code in the previous ticket, but adding a condition.
The condition could be: if the months and days of the birthday are < than the today date then display $age-1 else display $age.
But I don't now to write that.
Minesh
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
Asia/Kolkata (GMT+05:30)
Ok - so you should use the following shortcode where you need to use the floor function to round down result value to nearest smallest integer:
add_shortcode( 'iam-eta', 'wpv_post_age_shortcode');
function wpv_post_age_shortcode( $atts ) {
global $post;
$timestamp = get_post_meta( $post->ID, 'wpcf-data-di-nascita', true );
$age = floor((time() - $timestamp) / ( 60*60*24* 365 )) ;
return $age;
}
You need to use floor function:
=> lien caché
My issue is resolved now. Thank you!