2016-08-29 12 views
0

私はWordPressを使用しています。すべての投稿で年齢を動的に更新します。PHPレンダリング年齢

私は私のfunctions.phpに以下のコードを使用しています:

function internoetics_determine_age($atts) { 
extract(shortcode_atts(array(
'dob' => '' /* See post for date formats */ 
), $atts)); 
$age = floor((time() - strtotime($dob))/31556926); 
return $age; 
} 
add_shortcode('age', 'internoetics_determine_age'); 

私はその後、[年齢DOB =「1945」]ショートコードを使用しますが、表示された結果は-16で、それは71でなければなりません。

アイデア?

答えて

1

strtotime機能は日付と同じですので、strtotime(1945)は間違っています。 strtotime('1945-01-01')はとても正しいです:

$age = floor((time() - strtotime(((int)$dob) . '-01-01'))/31556926); 

しかし、あなただけ使用する必要があります。

$age = date('Y') - ((int)$dob); 

*((int型)$のDOBを)$のDOBは、異なるフォーマットを持っているだけの場合には

+0

何それは '[ある場合age dob = "1945-01-04"] '? –

+0

'$ dob =(int)$ dob;' – nospor