0

ExpressionEngine サイトで生年月日から人の年齢を計算しようとしています。次のコードは私のローカル テスト サイトで動作しますが、サーバーは古いバージョンの PHP (5.2.17) を使用しているため、エラーが発生します。誰かが代わりに使用する必要があるコードを提案できますか?

{exp:channel:entries channel='zoo_visitor'}
<?php
$dob = new DateTime('{member_birthday format='%Y-%m-%d'}');
$now = new DateTime('now');
// This returns a DateInterval object.
$age = $now->diff($dob);
// You can output the date difference however you choose.
echo 'This person is ' .$age->format('%y') .' years old.';
?>
{/exp:channel:entries}
4

7 に答える 7

3

DateTime::diffは PHP 5.3.0 で導入されたため、現在のコードは機能しません。

タイムゾーン、DST、うるう年を考慮に入れる必要があるため、通常、日付の計算は非常に複雑ですが、「年全体」の差を計算するような単純なタスクであれば、非常に簡単に計算できます。

アイデアは、結果が終了日の年から開始日の年を引いたものに等しいということです。開始日の月/日が終了日の年よりも早い場合は、そこから 1 を引く必要があります。コード:

$dob = new DateTime('24 June 1940');
$now = new DateTime('now');

echo year_diff($now, $dob);

function year_diff($date1, $date2) {
    list($year1, $dayOfYear1) = explode(' ', $date1->format('Y z'));
    list($year2, $dayOfYear2) = explode(' ', $date2->format('Y z'));
    return $year1 - $year2 - ($dayOfYear1 < $dayOfYear2);
}

実際に見てください。誕生日に指定されたのとまったく同じ日に結果が 1 増加することに注意してください。

于 2012-06-27T09:31:41.967 に答える
2
$dob = strtotime('{member_birthday format='%Y-%m-%d'}');
$now = time();
echo 'This person is ' . (1970 - date('Y', ($now - $dob))) .' years old.';
于 2012-06-27T09:24:38.647 に答える
1

diffは PHP 5.3 以降でのみ使用できます。

変更して試すことができます。5.2で動作します

$age = $now->modify('-' . $dob->format('Y') . 'year');
于 2012-06-27T09:23:07.340 に答える
1

多くの検索の後、私は答えを見つけました:

<?php
    //date in mm/dd/yyyy format
    $birthDate = "{member_birthday format='%m/%d/%Y'}";
    //explode the date to get month, day and year
    $birthDate = explode("/", $birthDate);
    //get age from date or birthdate
    $age = (date("md", 
                 date("U", 
                      mktime(0, 
                             0, 
                             0, 
                             $birthDate[0], 
                             $birthDate[1], 
                             $birthDate[2])
                     )
                 )
            > date("md") 
            ? ((date("Y") - $birthDate[2]) - 1)
            : (date("Y") - $birthDate[2]));
    echo $age;
?>   
于 2012-06-28T13:05:44.927 に答える
0

通年日のみを使用する計算は、いくつかのまれなケースで 1 ずれています: 2012 年 2 月 29 日と 2011 年 3 月 1 日は 1 年と表示されますが、これは 0 年 (および 11 か月と 28 日) である必要があります。うるう年を考慮した可能な解決策は次のとおりです。

<?php
    function calculateAge(DateTime $birthDate, DateTime $now = null) {
        if ($now == null) {
            $now = new DateTime;
        }

        $age = $now->format('Y') - $birthDate->format('Y');
        $dm = $now->format('m') - $birthDate->format('m');
        $dd = $now->format('d') - $birthDate->format('d');

        if ($dm < 0 || ($dm == 0 && $dd < 0)) {
            $age--;
        }

        return $age;
    }

    echo calculateAge(new DateTime('2011-04-01'), new DateTime('2012-03-29'));

ただし、user579984 のソリューションも機能します。

于 2013-01-18T10:35:50.240 に答える
0
$birthday = '1983-03-25';
$cm = date('Y', strtotime($birthday));
$cd = date('Y', strtotime('now'));

$res = $cd - $cm;

if (date('m', strtotime($birthday)) > date('m', strtotime('now')))
    $res--;
else if ((date('m', strtotime($birthday)) == date('m', strtotime('now'))) &&
    (date('d', strtotime($birthday)) > date('d', strtotime('now'))))
    $res--;

echo $res;
于 2013-01-18T12:51:42.330 に答える