이번년도와 지난년도 함수를 알고 싶어요

이번년도와 지난년도 함수를 알고 싶어요

QA

이번년도와 지난년도 함수를 알고 싶어요

답변 4

본문

기간 검색에서 날짜를 입력하는데

 

오늘의 값은 <?php echo G5_TIME_YMD; ?> 이렇게 넣고 있습니다.

올해 시작년도는 그냥 2021-01-01 이렇게 넣고

지난 년도는 2020-01-01    2020-12-31  이렇게 넣고 있는데

 

이것도 함수로 짤수 있나요?

 

막코딩 안하고 함수로 넣고 싶어요

이 질문에 댓글 쓰기 :

답변 4

date('Y-m-d', strtotime('first day of january this year'));
date('Y-m-d', strtotime('last day of december this year'));
date('Y-m-d', strtotime('first day of january last year'));
date('Y-m-d', strtotime('last day of december last year'));


function setdate1(){
  $time = time();

 return date("Y-m-d",strtotime("+12 month", $time)); // 1년후

}
 
function setdate2(){
  $time = time();

 return date("Y-m-d",strtotime("-12 month", $time)); // 1년전

}

 

이렇게 하셔도 되고

 

하나의 함수안에서 분기로 예외처리 하셔도 됩니다

 


function setdate($t){
  $time = time();
 if($t ==1){
return date("Y-m-d",strtotime("+12 month", $time)); // 1년후
}elseif($t==2){
return date("Y-m-d",strtotime("-12 month", $time)); // 1년전
}
}

작년과 올해의 년도만 구하실꺼면



date("Y",strtotime("-12 month", time()))."01-01"; //작년 1월1일

date("Y",strtotime("-12 month", time()))."12-31"; //작년 12월31일

 

이런식으로 하시면 됩니다

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
전체 16,797
© SIRSOFT
현재 페이지 제일 처음으로