wordpress - PHP Rendering Age -
i'm using wordpress , want age update dynamically in posts.
i using below code in functions.php:
function internoetics_determine_age($atts) { extract( shortcode_atts( array( 'dob' => '' /* see post date formats */ ), $atts ) ); $age = floor((time() - strtotime($dob)) / 31556926); return $age; } add_shortcode('age', 'internoetics_determine_age'); i use shortcode [age dob="1945"] result displayed -16, should 71.
any ideas?
strtotime function works dates strtotime(1945) incorrect. strtotime('1945-01-01') correct so:
$age = floor((time() - strtotime(((int)$dob) . '-01-01')) / 31556926); but should use:
$age = date('y') - ((int)$dob); *((int)$dob) in case $dob has different format
Comments
Post a Comment