PHP
[PHP] 유용한 실무팁4 - 최근 5년간 자동 Select박스 만들기, Timestamp 활용
http://portfolio.wonpaper.net
2023. 5. 16. 22:06
실무상에 자주 이용되는 부분이다.
목록 프로그램 짤때 최근 5년간 자동 Select 박스 만들기를 해보자.
이것도 Timestamp 숫자값을 이용한다.
올해를 기준으로 과거 -5년의 Timestamp 값을 얻어 오고 이를 date() 함수로 변환시켜 Select 박스를 만든다.
필자의 간단한 소스는 아래와 같다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<?
$prevYear = strtotime("-5 years"); // 과거 5년전의 Timestamp
$fstart = intval(date('Y'));
$fend = intval(date('Y',$prevYear));
?>
<select id="sYear" name="sYear" style="width:100px" onchange="syearChg()">
<?
for ($i=$fstart;$i>=$fend;$i--) {
?>
<option value="<?=$i?>" <?if ($i==date('Y')) echo "selected";?>><?=$i?>년</option>
<?
}
?>
</select>
<select id="sMonth" name="sMonth" style="width:100px" onchange="smonthChg()">
<option value="">=전체=</option>
<?
for ($i=1;$i<=12;$i++) {
if ($i < 10) {
$ii = "0" . $i;
} else {
$ii = $i;
}
?>
<option value="<?=$ii?>" <?if ($i==date('n')) echo "selected";?>><?=$i?>월</option>
<?
}
?>
</select>
|
cs |
참고 : https://blog.munilive.com/posts/In-strtotime-1-months-or-1-month-ago-is-not-30-days-old.html
참고 : https://extbrain.tistory.com/29