[PHP] 날짜 계산 연습
Lpla
·2021. 1. 24. 00:23
반응형
날짜를 계산하여 서로 다른 HTML값을 출력하기 위한 php 구문
PHP
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="con">
<!-- 시간, 분, 초까지 포함 -->
<?php
$end_date1 = date('2021-01-20 00:10:01'); //디데이 날짜
$d_day1 = floor((strtotime($end_date1) -
strtotime(date('Y-m-d H:i:s'))) / 86400);
if ($d_day1 < 0) {
$d_day1 = '<div class="event_btn event_end">이벤트 종료</div>';
} else {
$d_day1 = '<div class="event_btn event_ongoing">D-' . $d_day1 . '</div>';
}
echo $d_day1;
?>
<!-- 일까지만 포함 -->
<?php
$end_date2 = date('2022-01-20'); //디데이 날짜
$d_day2 = floor((strtotime($end_date2) -
strtotime(date('Y-m-d'))) / 86400);
if ($d_day2 < 0) {
$d_day2 = '<div class="event_btn event_end">이벤트 종료</div>';
} else {
$d_day2 = '<div class="event_btn event_ongoing">D-' . $d_day2 . '</div>';
}
echo $d_day2;
?>
</div>
</body>
</html>
CSS
.con { padding: 50px; }
.event_btn { width: 180px; margin: 10px auto; padding: 4px 0; border-radius: 3px; text-align: center; }
.event_ongoing { color: #fff; background: #3498db; }
.event_end { color: #fff; background: #9e9e9e; }
결과
출력 HTML
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="con">
<!-- 시간, 분, 초까지 포함 -->
<div class="event_btn event_end">이벤트 종료</div>
<!-- 일까지만 포함 -->
<div class="event_btn event_ongoing">D-361</div>
</div>
</body>
</html>
반응형