답변 3개 / 댓글 1개
채택된 답변
+20 포인트
이런식으로 함수를 만드시면 됩니다.
Copy
function getAgeGroup($birthdate) {
// 생년월일에서 연도, 월, 일을 분리
$birthYear = substr($birthdate, 0, 4);
$birthMonth = substr($birthdate, 4, 2);
$birthDay = substr($birthdate, 6, 2);
// 현재 날짜 구하기
$currentYear = date("Y");
$currentMonth = date("m");
$currentDay = date("d");
// 만 나이 계산
$age = $currentYear - $birthYear;
if ($currentMonth < $birthMonth || ($currentMonth == $birthMonth && $currentDay < $birthDay)) {
$age--;
}
// 나이대 구분
if ($age >= 10 && $age < 20) {
return "10대";
} elseif ($age >= 20 && $age < 30) {
return "20대";
} elseif ($age >= 30 && $age < 40) {
return "30대";
} elseif ($age >= 40 && $age < 50) {
return "40대";
} elseif ($age >= 50 && $age < 60) {
return "50대";
} else {
return "10대 미만 또는 60대 이상";
}
}
// 예시 사용법
$birthdate = "19991020";
$ageGroup = getAgeGroup($birthdate);
답변을 작성하려면 로그인이 필요합니다.