web-dev-qa-db-ja.com

生年月日に基づいて年齢を計算する

Sqlにユーザーの表があり、それぞれに誕生日があります。生年月日を年齢(年のみ)に変換したい日付:15.03.1999年齢:14および15.03.2014は年齢:15に変更されます

ここで、ユーザーの日付を表示します。

if(isset($_GET['id']))
{
    $id = intval($_GET['id']);
    $dnn = mysql_fetch_array($dn);
    $dn = mysql_query('select username, email, skype, avatar, ' .
        'date, signup_date, gender from users where id="'.$id.'"');
    $dnn = mysql_fetch_array($dn);
    echo "{$dnn['date']}";
}
60
PHPupil

PHP > = 5.3.0

# object oriented
$from = new DateTime('1970-02-01');
$to   = new DateTime('today');
echo $from->diff($to)->y;

# procedural
echo date_diff(date_create('1970-02-01'), date_create('today'))->y;

demo

関数: date_create()date_diff()


MySQL > = 5.0.0

SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age

demo

関数: TIMESTAMPDIFF()CURDATE()

176
Glavić

年齢を取得するための非常に小さなコード:

<?php
    $dob='1981-10-07';
    $diff = (date('Y') - date('Y',strtotime($dob)));
    echo $diff;
?>

//output 35
10
easycodingclub

ネットからこのスクリプトを入手しました(coffeecupwebに感謝)

<?php
/**
 * Simple PHP age Calculator
 * 
 * Calculate and returns age based on the date provided by the user.
 * @param   date of birth('Format:yyyy-mm-dd').
 * @return  age based on date of birth
 */
function ageCalculator($dob){
    if(!empty($dob)){
        $birthdate = new DateTime($dob);
        $today   = new DateTime('today');
        $age = $birthdate->diff($today)->y;
        return $age;
    }else{
        return 0;
    }
}
$dob = '1992-03-18';
echo ageCalculator($dob);
?>
4
googli.us

参照リンク http://www.calculator.net/age-calculator.html

$hours_in_day   = 24;
$minutes_in_hour= 60;
$seconds_in_mins= 60;

$birth_date     = new DateTime("1988-07-31T00:00:00");
$current_date   = new DateTime();

$diff           = $birth_date->diff($current_date);

echo $years     = $diff->y . " years " . $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>";
echo $months    = ($diff->y * 12) + $diff->m . " months " . $diff->d . " day(s)"; echo "<br/>";
echo $weeks     = floor($diff->days/7) . " weeks " . $diff->d%7 . " day(s)"; echo "<br/>";
echo $days      = $diff->days . " days"; echo "<br/>";
echo $hours     = $diff->h + ($diff->days * $hours_in_day) . " hours"; echo "<br/>";
echo $mins      = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour) . " minutest"; echo "<br/>";
echo $seconds   = $diff->h + ($diff->days * $hours_in_day * $minutes_in_hour * $seconds_in_mins) . " seconds"; echo "<br/>";
3
Shailesh Sonare
 $dob = $this->dateOfBirth; //Datetime 
        $currentDate = new \DateTime();
        $dateDiff = $dob->diff($currentDate);
        $years = $dateDiff->y;
        $months = $dateDiff->m;
        $days = $dateDiff->d;
        $age = $years .' Year(s)';

        if($years === 0) {
            $age = $months .' Month(s)';
            if($months === 0) {
                $age = $days .' Day(s)';
            }
        }
        return $age;
0
Rutendo

生年月日から年齢を計算します。

$dob = '1991-09-30';
(((int) date("m",strtotime($dob)) >= (int) date('m')) && ((int) date("d",strtotime($dob)) >= (int) date('d'))) 

    ? 
    $age = (date('Y') - date('Y',strtotime($dob))) 
    :  
    $age = (date('Y') - date('Y',strtotime($dob)))-1;

出力:26

0
Shaan Ansari

@dateOfBirth dateselect @dateOfBirth = '2000-01-01'を宣言します

SELECT datediff(YEAR,@dateOfBirth,getdate()) as Age

0
YATHI

日付/月/年形式の誕生日の場合

function age($birthday){
 list($day, $month, $year) = explode("/", $birthday);
 $year_diff  = date("Y") - $year;
 $month_diff = date("m") - $month;
 $day_diff   = date("d") - $day;
 if ($day_diff < 0 && $month_diff==0) $year_diff--;
 if ($day_diff < 0 && $month_diff < 0) $year_diff--;
 return $year_diff;
}

または、パラメーターとして日、月、年を受け入れる同じ関数:

function age($day, $month, $year){
 $year_diff  = date("Y") - $year;
 $month_diff = date("m") - $month;
 $day_diff   = date("d") - $day;
 if ($day_diff < 0 && $month_diff==0) $year_diff--;
 if ($day_diff < 0 && $month_diff < 0) $year_diff--;
 return $year_diff;
}

次のように使用できます。

echo age("20/01/2000");

正しい年齢が出力されます(6月4日、14です)。

0
Subin
$getyear = explode("-", $value['users_dob']);
$dob = date('Y') - $getyear[0];

$value['users_dob']は、yyyy-mm-dd形式のデータベース値です

0
Tayyab Hayat

これがあなたのお役に立てば幸いです。

$query1="SELECT TIMESTAMPDIFF (YEAR, YOUR_DOB_COLUMN, CURDATE()) AS age FROM your_table WHERE id='$user_id'";
$res1=mysql_query($query1);
$row=mysql_fetch_array($res1);
echo $row['age'];
0
Sushant Samleti