-1

重複の可能性:
人の年齢を年、月、日で計算するにはどうすればよいですか?

生年月日と現在の日付が与えられた人の年齢を、現在の日付からの年、月、日で計算したいと考えています。

例えば:

>>> calculate_age(2008, 01, 01)
1 years, 0 months, 16 days
4

4 に答える 4

32

PHP date_diff http://php.net/manual/en/function.date-diff.phpまたはhttp://www.php.net/manual/en/datetime.diff.phpを使用して、目的を達成することができます。PHP日付形式を使用して、任意の形式で出力できます

$interval = date_diff(date_create(), date_create('2008-01-01 10:30:00'));
echo $interval->format("You are  %Y Year, %M Months, %d Days, %H Hours, %i Minutes, %s Seconds Old");

エコー

You are 04 Year, 04 Months, 1 Days, 00 Hours, 56 Minutes, 36 Seconds Old
于 2012-05-02T09:27:40.187 に答える
3
<?php
date_default_timezone_set('Asia/Calcutta');

function findage($dob)
{
    $localtime = getdate();
    $today = $localtime['mday']."-".$localtime['mon']."-".$localtime['year'];
    $dob_a = explode("-", $dob);
    $today_a = explode("-", $today);
    $dob_d = $dob_a[0];$dob_m = $dob_a[1];$dob_y = $dob_a[2];
    $today_d = $today_a[0];$today_m = $today_a[1];$today_y = $today_a[2];
    $years = $today_y - $dob_y;
    $months = $today_m - $dob_m;
    if ($today_m.$today_d < $dob_m.$dob_d) 
    {
        $years--;
        $months = 12 + $today_m - $dob_m;
    }

    if ($today_d < $dob_d) 
    {
        $months--;
    }

    $firstMonths=array(1,3,5,7,8,10,12);
    $secondMonths=array(4,6,9,11);
    $thirdMonths=array(2);

    if($today_m - $dob_m == 1) 
    {
        if(in_array($dob_m, $firstMonths)) 
        {
            array_push($firstMonths, 0);
        }
        elseif(in_array($dob_m, $secondMonths)) 
        {
            array_push($secondMonths, 0);
        }elseif(in_array($dob_m, $thirdMonths)) 
        {
            array_push($thirdMonths, 0);
        }
    }
    echo "<br><br> Age is $years years $months months.";
}

findage("21-04-1969"); //put date in the dd-mm-yyyy format
?>
于 2012-05-02T09:35:32.973 に答える
1
<?php
$ageTime = mktime(0, 0, 0, 9, 9, 1919); // Get the person's birthday timestamp
$t = time(); // Store current time for consistency
$age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
$year = 60 * 60 * 24 * 365;
$ageYears = $age / $year;

echo 'You are ' . floor($ageYears) . ' years old.';
?>

ソース

于 2012-05-02T09:21:51.600 に答える
0

datatimediff関数を使用する

http://php.net/manual/en/datetime.diff.php

于 2012-05-02T09:28:21.383 に答える