PHP

[PHP] 날짜 관련 함수 모음 - 해당 월 첫째날, 마지막날, 몇 주차

http://portfolio.wonpaper.net 2024. 6. 25. 19:10

 

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
36
37
38
39
40
41
<?
echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>\n";
date_default_timezone_set('Asia/Seoul'); // 시간대를 서울로 설정
 
// 현재 날짜 가져오기
$currentDate = time();
$month = date('n'$currentDate);
$year = date('Y'$currentDate);
 
// 해당 월의 첫째 날
$firstDayOfMonth = strtotime("$year-$month-01");
 
// 해당 월의 마지막 날
$lastDayOfMonth = strtotime(date('Y-m-t'$firstDayOfMonth));
 
// 첫째 날의 주차
$firstDayOfMonthWeek = date('W'$firstDayOfMonth);
$currentWeek = date('W'$currentDate);
$currentWeekOfMonth = $currentWeek - $firstDayOfMonthWeek + 1;
 
// 주차 출력
echo "이번 달의 현재 주차: " . $currentWeekOfMonth . "주차<br>";
 
// 현재 주의 일요일부터 토요일까지의 일자 출력
$startOfWeek = strtotime('Monday this week'$currentDate);
 
echo "<br><br>" . $startOfWeek . "<br><br>";
 
$yoil = array("일","월","화","수","목","금","토");
echo $yoil[date('w', strtotime(date('Y-m-d')))] ;
echo "<br><br>";
 
 
echo "월요일부터 일요일까지의 일자 :<br><br>";
for ($i = 0$i < 7$i++) {
    echo date('Y-m-d (l)'$startOfWeek) . "<br>";
    $startOfWeek = strtotime('+1 day'$startOfWeek);
}
 
 
?>
cs