PHP 특정일의 주차, 요일 / 해당주의 시작일, 종료일
페이지 정보
본문
주의 기간은 일요일~토요일 입니다
요일계산 : 일 = 0, 월 = 1, 화 = 2, 수 = 3, 목 = 4, 금 = 5, 토 = 6 입니다.
테스트 : https://www.happyjung.com/demo/php/todayOfTheWeek.php
<?php
echo " 2021-01-01 = 2020년도 53주 금요일(5) 2020-12-27~2021-01-02 <br>";
echo " 2021-01-02 = 2020년도 53주 토요일(6) 2020-12-27~2021-01-02 <br>";
echo " 2021-01-03 = 2020년도 53주 일요일(0) 2021-01-03~2021-01-09 <br>";
echo " 2021-01-04 = 2021년도 01주 월요일(1) 2021-01-03~2021-01-09 <br>";
echo "<br>";
//$today_year = date('Y');
//$today_month = date('m');
//$today_day = date('d');
$today_year = '2021';
$today_month = '01';
$today_day = '03';
// 오늘날짜
$today_date = $today_year ."-". $today_month ."-". $today_day;
//today_date = 20210103
// 해당요일
$todayOfTheWeek1 = date('w',mktime(0,0,0,$today_month,$today_day,$today_year));
//todayOfTheWeek1 = 0
$todayOfTheWeek2 = date('w',strtotime($today_year.$today_month.$today_day));
//todayOfTheWeek2 = 0
// 요일 한글로 변환
if ($todayOfTheWeek1=="0") { $todayOfTheWeek_kor = "일"; $todayOfTheWeek_eng = "SUN"; }
elseif ($todayOfTheWeek1=="1") { $todayOfTheWeek_kor = "월"; $todayOfTheWeek_eng = "MON"; }
elseif ($todayOfTheWeek1=="2") { $todayOfTheWeek_kor = "화"; $todayOfTheWeek_eng = "TUE"; }
elseif ($todayOfTheWeek1=="3") { $todayOfTheWeek_kor = "수"; $todayOfTheWeek_eng = "WED"; }
elseif ($todayOfTheWeek1=="4") { $todayOfTheWeek_kor = "목"; $todayOfTheWeek_eng = "THU"; }
elseif ($todayOfTheWeek1=="5") { $todayOfTheWeek_kor = "금"; $todayOfTheWeek_eng = "FRI"; }
elseif ($todayOfTheWeek1=="6") { $todayOfTheWeek_kor = "토"; $todayOfTheWeek_eng = "SAT";}
// 주차구하기
$today_week1 = date('W',mktime(0,0,0,$today_month,$today_day,$today_year));
//today_week1 = 53
$today_week2 = date('W',strtotime($today_year.$today_month.$today_day));
//today_week2 = 53
// 해당주차의 시작 날짜
$today_week_sday1 = mktime(0,0,0,$today_month,$today_day-$todayOfTheWeek1,$today_year);
//today_week_sday1 = 1609599600
$today_week_sday2 = date("Y-m-d H:i:s",$today_week_sday1);
//today_week_sday2 = 2021-01-03 00:00:00
// 해당주차의 종료 날짜
$today_week_eday1 = mktime(23,59,59,$today_month,$today_day+(6-$todayOfTheWeek1),$today_year);
//today_week_eday1 = 1610204399
$today_week_eday2 = date("Y-m-d H:i:s",$today_week_eday1);
//today_week_eday2 = 2021-01-09 23:59:59
// 해당달의 시작 날짜
$today_week_month_sday1 = mktime(23,59,59,$today_month,1,$today_year);
//today_week_month_sday = 1609513199
$today_week_month_sday2 = date("Y-m-d H:i:s",$today_week_month_sday1);
//today_week_month_sday2 = 2021-01-01 23:59:59
// 해당월의 마지막 날짜
$today_week_month_eday1 = mktime(23,59,59,$today_month,date("t",$today_week_month_sday1),$today_year);
//today_week_month_eday1 = 1612105199
$today_week_month_eday2 = date("Y-m-d H:i:s",$today_week_month_eday1);
//today_week_month_eday2 = 2021-01-31 23:59:59
// 년도==12 & 주차=='01' 이면 디비저장년도=년도+1
if($today_month==12 & $today_week1=='01') { $today_year_db = $today_year+1; } // 년도 + 1
elseif($today_month==01 & $today_week1>'01') { $today_year_db = $today_year-1; } // 년도 - 1
else { $today_year_db = $today_year; }
if ($todayOfTheWeek1=="0") { // 일요일일때는 주차 - 1
// 디비저장 해당주차의 시작 날짜 (월요일)
$today_week_sday_db = date("Y-m-d H:i:s",strtotime("-6 day", $today_week_sday1)); //$today_week_sday_db = 2020-12-28 00:00:00
//echo "today_week_sday_db = ".$today_week_sday_db."<br>";
// 디비저장 해당주차의 마지막 날짜 (일요일)
$today_week_eday_db = date("Y-m-d H:i:s",strtotime("-6 day", $today_week_eday1)); //$today_week_eday_db = 2021-01-03 23:59:59
//echo "today_week_eday_db = ".$today_week_eday_db."<br>";
} else {
// 디비저장 해당주차의 시작 날짜 (월요일)
$today_week_sday_db = date("Y-m-d H:i:s",strtotime("1 day", $today_week_sday1)); //$today_week_sday_db = 2020-12-28 00:00:00
//echo "today_week_sday_db = ".$today_week_sday_db."<br>";
// 디비저장 해당주차의 마지막 날짜 (일요일)
$today_week_eday_db = date("Y-m-d H:i:s",strtotime("1 day", $today_week_eday1)); //$today_week_eday_db = 2021-01-03 23:59:59
//echo "today_week_eday_db = ".$today_week_eday_db."<br>";
}
echo "오늘날짜 = ". $today_date ."<br>";
echo "<br>";
echo "해당요일<br>";
echo "todayOfTheWeek1 = ". $todayOfTheWeek1 ."<br>";
echo "todayOfTheWeek2 = ". $todayOfTheWeek2 ."<br>";
echo "<br>";
echo "주차구하기<br>";
echo "today_week1 = ". $today_week1 ."<br>";
echo "today_week2 = ". $today_week2 ."<br>";
echo "<br>";
echo "해당주차의 시작 날짜<br>";
echo "today_week_sday1 = ". $today_week_sday1 ."<br>";
echo "today_week_sday2 = ". $today_week_sday2 ."<br>";
echo "<br>";
echo "해당주차의 마지막 날짜<br>";
echo "today_week_eday1 = ". $today_week_eday1 ."<br>";
echo "today_week_eday2 = ". $today_week_eday2 ."<br>";
echo "<br>";
echo "해당월의 시작 날짜<br>";
echo "today_week_month_sday1 = ". $today_week_month_sday1 ."<br>";
echo "today_week_month_sday2 = ". $today_week_month_sday2 ."<br>";
echo "<br>";
echo "해당월의 마지막 날짜<br>";
echo "today_week_month_eday1 = ". $today_week_month_eday1 ."<br>";
echo "today_week_month_eday2 = ". $today_week_month_eday2 ."<br>";
echo "<br>";
echo "디비저장<br>";
echo "년도 = ". $today_year_db ."<br>";
echo "주차 = ". $today_week1 ."<br>";
echo "날짜 = ". $today_year . $today_month . $today_day ."<br>";
echo "요일 = ". $todayOfTheWeek1 ."<br>";
echo "요일(한글) = ". $todayOfTheWeek_kor ."<br>";
echo "주시작(월) = ". $today_week_sday_db ."<br>";
echo "주종료(일) = ". $today_week_eday_db ."<br>";
?>
결과값
2021-01-01 = 2020년도 53주 금요일(5) 2020-12-27~2021-01-02
2021-01-02 = 2020년도 53주 토요일(6) 2020-12-27~2021-01-02
2021-01-03 = 2020년도 53주 일요일(0) 2021-01-03~2021-01-09
2021-01-04 = 2021년도 01주 월요일(1) 2021-01-03~2021-01-09
오늘날짜 = 2021-01-03
해당요일
todayOfTheWeek1 = 0
todayOfTheWeek2 = 0
주차구하기
today_week1 = 53
today_week2 = 53
해당주차의 시작 날짜
today_week_sday1 = 1609599600
today_week_sday2 = 2021-01-03 00:00:00
해당주차의 마지막 날짜
today_week_eday1 = 1610204399
today_week_eday2 = 2021-01-09 23:59:59
해당월의 시작 날짜
today_week_month_sday1 = 1609513199
today_week_month_sday2 = 2021-01-01 23:59:59
해당월의 마지막 날짜
today_week_month_eday1 = 1612105199
today_week_month_eday2 = 2021-01-31 23:59:59
디비저장
년도 = 2020
주차 = 53
날짜 = 20210103
요일 = 0
요일(한글) = 일
주시작(월) = 2020-12-28 00:00:00
주종료(일) = 2021-01-03 23:59:59
참고자료
https://www.funyphp.com/archive/php/15
요일계산 : 일 = 0, 월 = 1, 화 = 2, 수 = 3, 목 = 4, 금 = 5, 토 = 6 입니다.
테스트 : https://www.happyjung.com/demo/php/todayOfTheWeek.php
<?php
echo " 2021-01-01 = 2020년도 53주 금요일(5) 2020-12-27~2021-01-02 <br>";
echo " 2021-01-02 = 2020년도 53주 토요일(6) 2020-12-27~2021-01-02 <br>";
echo " 2021-01-03 = 2020년도 53주 일요일(0) 2021-01-03~2021-01-09 <br>";
echo " 2021-01-04 = 2021년도 01주 월요일(1) 2021-01-03~2021-01-09 <br>";
echo "<br>";
//$today_year = date('Y');
//$today_month = date('m');
//$today_day = date('d');
$today_year = '2021';
$today_month = '01';
$today_day = '03';
// 오늘날짜
$today_date = $today_year ."-". $today_month ."-". $today_day;
//today_date = 20210103
// 해당요일
$todayOfTheWeek1 = date('w',mktime(0,0,0,$today_month,$today_day,$today_year));
//todayOfTheWeek1 = 0
$todayOfTheWeek2 = date('w',strtotime($today_year.$today_month.$today_day));
//todayOfTheWeek2 = 0
// 요일 한글로 변환
if ($todayOfTheWeek1=="0") { $todayOfTheWeek_kor = "일"; $todayOfTheWeek_eng = "SUN"; }
elseif ($todayOfTheWeek1=="1") { $todayOfTheWeek_kor = "월"; $todayOfTheWeek_eng = "MON"; }
elseif ($todayOfTheWeek1=="2") { $todayOfTheWeek_kor = "화"; $todayOfTheWeek_eng = "TUE"; }
elseif ($todayOfTheWeek1=="3") { $todayOfTheWeek_kor = "수"; $todayOfTheWeek_eng = "WED"; }
elseif ($todayOfTheWeek1=="4") { $todayOfTheWeek_kor = "목"; $todayOfTheWeek_eng = "THU"; }
elseif ($todayOfTheWeek1=="5") { $todayOfTheWeek_kor = "금"; $todayOfTheWeek_eng = "FRI"; }
elseif ($todayOfTheWeek1=="6") { $todayOfTheWeek_kor = "토"; $todayOfTheWeek_eng = "SAT";}
// 주차구하기
$today_week1 = date('W',mktime(0,0,0,$today_month,$today_day,$today_year));
//today_week1 = 53
$today_week2 = date('W',strtotime($today_year.$today_month.$today_day));
//today_week2 = 53
// 해당주차의 시작 날짜
$today_week_sday1 = mktime(0,0,0,$today_month,$today_day-$todayOfTheWeek1,$today_year);
//today_week_sday1 = 1609599600
$today_week_sday2 = date("Y-m-d H:i:s",$today_week_sday1);
//today_week_sday2 = 2021-01-03 00:00:00
// 해당주차의 종료 날짜
$today_week_eday1 = mktime(23,59,59,$today_month,$today_day+(6-$todayOfTheWeek1),$today_year);
//today_week_eday1 = 1610204399
$today_week_eday2 = date("Y-m-d H:i:s",$today_week_eday1);
//today_week_eday2 = 2021-01-09 23:59:59
// 해당달의 시작 날짜
$today_week_month_sday1 = mktime(23,59,59,$today_month,1,$today_year);
//today_week_month_sday = 1609513199
$today_week_month_sday2 = date("Y-m-d H:i:s",$today_week_month_sday1);
//today_week_month_sday2 = 2021-01-01 23:59:59
// 해당월의 마지막 날짜
$today_week_month_eday1 = mktime(23,59,59,$today_month,date("t",$today_week_month_sday1),$today_year);
//today_week_month_eday1 = 1612105199
$today_week_month_eday2 = date("Y-m-d H:i:s",$today_week_month_eday1);
//today_week_month_eday2 = 2021-01-31 23:59:59
// 년도==12 & 주차=='01' 이면 디비저장년도=년도+1
if($today_month==12 & $today_week1=='01') { $today_year_db = $today_year+1; } // 년도 + 1
elseif($today_month==01 & $today_week1>'01') { $today_year_db = $today_year-1; } // 년도 - 1
else { $today_year_db = $today_year; }
if ($todayOfTheWeek1=="0") { // 일요일일때는 주차 - 1
// 디비저장 해당주차의 시작 날짜 (월요일)
$today_week_sday_db = date("Y-m-d H:i:s",strtotime("-6 day", $today_week_sday1)); //$today_week_sday_db = 2020-12-28 00:00:00
//echo "today_week_sday_db = ".$today_week_sday_db."<br>";
// 디비저장 해당주차의 마지막 날짜 (일요일)
$today_week_eday_db = date("Y-m-d H:i:s",strtotime("-6 day", $today_week_eday1)); //$today_week_eday_db = 2021-01-03 23:59:59
//echo "today_week_eday_db = ".$today_week_eday_db."<br>";
} else {
// 디비저장 해당주차의 시작 날짜 (월요일)
$today_week_sday_db = date("Y-m-d H:i:s",strtotime("1 day", $today_week_sday1)); //$today_week_sday_db = 2020-12-28 00:00:00
//echo "today_week_sday_db = ".$today_week_sday_db."<br>";
// 디비저장 해당주차의 마지막 날짜 (일요일)
$today_week_eday_db = date("Y-m-d H:i:s",strtotime("1 day", $today_week_eday1)); //$today_week_eday_db = 2021-01-03 23:59:59
//echo "today_week_eday_db = ".$today_week_eday_db."<br>";
}
echo "오늘날짜 = ". $today_date ."<br>";
echo "<br>";
echo "해당요일<br>";
echo "todayOfTheWeek1 = ". $todayOfTheWeek1 ."<br>";
echo "todayOfTheWeek2 = ". $todayOfTheWeek2 ."<br>";
echo "<br>";
echo "주차구하기<br>";
echo "today_week1 = ". $today_week1 ."<br>";
echo "today_week2 = ". $today_week2 ."<br>";
echo "<br>";
echo "해당주차의 시작 날짜<br>";
echo "today_week_sday1 = ". $today_week_sday1 ."<br>";
echo "today_week_sday2 = ". $today_week_sday2 ."<br>";
echo "<br>";
echo "해당주차의 마지막 날짜<br>";
echo "today_week_eday1 = ". $today_week_eday1 ."<br>";
echo "today_week_eday2 = ". $today_week_eday2 ."<br>";
echo "<br>";
echo "해당월의 시작 날짜<br>";
echo "today_week_month_sday1 = ". $today_week_month_sday1 ."<br>";
echo "today_week_month_sday2 = ". $today_week_month_sday2 ."<br>";
echo "<br>";
echo "해당월의 마지막 날짜<br>";
echo "today_week_month_eday1 = ". $today_week_month_eday1 ."<br>";
echo "today_week_month_eday2 = ". $today_week_month_eday2 ."<br>";
echo "<br>";
echo "디비저장<br>";
echo "년도 = ". $today_year_db ."<br>";
echo "주차 = ". $today_week1 ."<br>";
echo "날짜 = ". $today_year . $today_month . $today_day ."<br>";
echo "요일 = ". $todayOfTheWeek1 ."<br>";
echo "요일(한글) = ". $todayOfTheWeek_kor ."<br>";
echo "주시작(월) = ". $today_week_sday_db ."<br>";
echo "주종료(일) = ". $today_week_eday_db ."<br>";
?>
결과값
2021-01-01 = 2020년도 53주 금요일(5) 2020-12-27~2021-01-02
2021-01-02 = 2020년도 53주 토요일(6) 2020-12-27~2021-01-02
2021-01-03 = 2020년도 53주 일요일(0) 2021-01-03~2021-01-09
2021-01-04 = 2021년도 01주 월요일(1) 2021-01-03~2021-01-09
오늘날짜 = 2021-01-03
해당요일
todayOfTheWeek1 = 0
todayOfTheWeek2 = 0
주차구하기
today_week1 = 53
today_week2 = 53
해당주차의 시작 날짜
today_week_sday1 = 1609599600
today_week_sday2 = 2021-01-03 00:00:00
해당주차의 마지막 날짜
today_week_eday1 = 1610204399
today_week_eday2 = 2021-01-09 23:59:59
해당월의 시작 날짜
today_week_month_sday1 = 1609513199
today_week_month_sday2 = 2021-01-01 23:59:59
해당월의 마지막 날짜
today_week_month_eday1 = 1612105199
today_week_month_eday2 = 2021-01-31 23:59:59
디비저장
년도 = 2020
주차 = 53
날짜 = 20210103
요일 = 0
요일(한글) = 일
주시작(월) = 2020-12-28 00:00:00
주종료(일) = 2021-01-03 23:59:59
참고자료
https://www.funyphp.com/archive/php/15
댓글목록
등록된 댓글이 없습니다.