특정일 이후 한달뒤 계산
본문
date("Y-m-d", strtotime('+1 month'))
위 코드는 금일로 부터 1달뒤지만 특정일로 부터 한달뒤는 어떻게 구하나요?
예) 특정일 2025-08-18
2025-09-18 추출
답변 3
이렇게 하면 간단하게 됩니다
$originalDate = "2025-08-18";
echo $final = date("Y-m-d", strtotime("$originalDate +1 month"));
자문자답입니다.
$originalDate = "2025-08-18";
$newDate = date("Y-m-d", strtotime($originalDate));
$time = strtotime($newDate);
echo $final = date("Y-m-d", strtotime("+1 month", $time));
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>날짜 계산기</title>
<style>
body {
font-family: 'Segoe UI', sans-serif;
background: #f3f4f6;
padding: 30px;
text-align: center;
}
h1 {
color: #333;
}
input[type="date"] {
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 8px;
}
button {
margin-top: 15px;
padding: 10px 20px;
font-size: 16px;
background-color: #4f46e5;
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #4338ca;
}
.result {
margin-top: 30px;
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
display: inline-block;
text-align: left;
}
.result div {
margin: 8px 0;
}
</style>
</head>
<body>
<h1>? 날짜 계산기</h1>
<p>기준 날짜를 선택하세요:</p>
<input type="date" id="baseDate" />
<br/>
<button onclick="calculateDates()">날짜 계산하기</button>
<div class="result" id="resultBox" style="display:none;"></div>
<script>
function calculateDates() {
const base = document.getElementById('baseDate').value;
if (!base) return alert("날짜를 선택해주세요!");
const baseDate = new Date(base);
const resultBox = document.getElementById("resultBox");
const weekdays = ['일', '월', '화', '수', '목', '금', '토'];
const addDate = (d, days = 0, months = 0, years = 0) => {
const newDate = new Date(d);
newDate.setDate(newDate.getDate() + days);
newDate.setMonth(newDate.getMonth() + months);
newDate.setFullYear(newDate.getFullYear() + years);
return newDate;
}
const format = (d) =>
`${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')} (${weekdays[d.getDay()]})`;
const results = [
{ label: '7일 후', date: addDate(baseDate, 7) },
{ label: '보름 후 (15일)', date: addDate(baseDate, 15) },
{ label: '1개월 후', date: addDate(baseDate, 0, 1) },
{ label: '3개월 후', date: addDate(baseDate, 0, 3) },
{ label: '6개월 후', date: addDate(baseDate, 0, 6) },
{ label: '1년 후', date: addDate(baseDate, 0, 0, 1) },
{ label: '2년 후', date: addDate(baseDate, 0, 0, 2) }
];
resultBox.innerHTML = `<strong>? 기준일: ${format(baseDate)}</strong><br><hr>`;
results.forEach(item => {
resultBox.innerHTML += `<div>? ${item.label}: <strong>${format(item.date)}</strong></div>`;
});
resultBox.style.display = 'block';
}
</script>
</body>
</html>
답변을 작성하시기 전에 로그인 해주세요.