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