[PHP] 날짜 더하기 관련
PHP스쿨에 올라온 질문 중에서 "6개월 단위로 기간 연장하기"를 보고 관련 내용 적습니다.
어떤 날짜에서 얼마만큼 더하거나 뺀 날짜를 구하기.
mktime(), strtotime() 함수를 이용해 구해보겠습니다.
+ mktime()
- http://php.net/manual/en/function.mktime.php
+ strtotime()
- http://php.net/manual/en/function.strtotime.php
예를 위해 먼저 기준을 잡습니다.
'2012-10-31 14:23:55', timestamp[time()]는 1351661035입니다. (Asia/Seoul)
mktime()은 원하는 곳에 값을 더하거나 빼면 됩니다.
strtotime()은 문자열을 추가하면 됩니다.
기타 단순하게 초를 더하는 방법도 있습니다.
그런데!
2012년 10월 31일에서 한 달을 더한 결과를 보세요. '2012-12-01'이 나오죠?
2012년 11월 31일은 없죠? 30일까지 있죠? 그렇게 해서 다음 달로 넘어갑니다.
day에 대한 설명 : The number of the day relevant to the end of the previous month. Values 1 to 28, 29, 30 or 31 (depending upon the month) reference the normal days in the relevant month. Values less than 1 (including negative values) reference the days in the previous month, so 0 is the last day of the previous month, -1 is the day before that, etc. Values greater than the number of days in the relevant month reference the appropriate day in the following month(s).
이 부분을 해결하려면? 직접 만들면 그만이죠.
11월의 일 수는 30입니다. 2월을 제외하고 나머진 고정이지요.
12월 + 1달 = 13월
10월 + 6달 = 16월
먼저 위와 같이 12월을 넘는 경우의 처리를 해줘야 합니다.
간편하죠?
예제에선 10월 + 1달 = 11월이니까 해당 없죠? 남은 것은 말일 계산.
$last[11]은 30이죠. $d(31)가 크니까 마지막 날짜로 할당.
최종적으로 구한 $new 값은 '2012-11-30 14:30:50'입니다. 깔끔하죠?
Second, Minute, Hour, Day까지 신경쓰면 코드가 좀 길어지겠죠?
위에선 더한 경우만 들었지만 빼는 경우도 약간 차이나고요.
기타 생략합니다. 차근차근 구현해보세요.
‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥
Copy&Paste만 좋아하는 사람들, 간편한 것을 좋아하는 사람들에게 유용한
DB 함수를 사용한 방법 덧붙입니다.
- http://www.mysqlkorea.co.kr/sub.html?mcode=manual&scode=01&m_no=21728&cat1=12&cat2=355&cat3=0&lang=k
- http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
PHP 코드 생략합니다.
어떤 날짜에서 얼마만큼 더하거나 뺀 날짜를 구하기.
mktime(), strtotime() 함수를 이용해 구해보겠습니다.
+ mktime()
- http://php.net/manual/en/function.mktime.php
+ strtotime()
- http://php.net/manual/en/function.strtotime.php
예를 위해 먼저 기준을 잡습니다.
'2012-10-31 14:23:55', timestamp[time()]는 1351661035입니다. (Asia/Seoul)
mktime()은 원하는 곳에 값을 더하거나 빼면 됩니다.
echo date('Y-m-d H:i:s',mktime(14+1,30,50,10,31,2012)); // 2012-10-31 15:30:50
echo date('Y-m-d H:i:s',mktime(14,30+1,50,10,31,2012)); // 2012-10-31 14:31:50
echo date('Y-m-d H:i:s',mktime(14,30,50+1,10,31,2012)); // 2012-10-31 14:30:51
echo date('Y-m-d H:i:s',mktime(14,30,50,10+1,31,2012)); // 2012-12-01 14:30:50 ※
echo date('Y-m-d H:i:s',mktime(14,30,50,10,31+1,2012)); // 2012-11-01 14:30:50
echo date('Y-m-d H:i:s',mktime(14,30,50,10,31,2012+1)); // 2013-10-31 14:30:50
echo date('Y-m-d H:i:s',mktime(14,30+1,50,10,31,2012)); // 2012-10-31 14:31:50
echo date('Y-m-d H:i:s',mktime(14,30,50+1,10,31,2012)); // 2012-10-31 14:30:51
echo date('Y-m-d H:i:s',mktime(14,30,50,10+1,31,2012)); // 2012-12-01 14:30:50 ※
echo date('Y-m-d H:i:s',mktime(14,30,50,10,31+1,2012)); // 2012-11-01 14:30:50
echo date('Y-m-d H:i:s',mktime(14,30,50,10,31,2012+1)); // 2013-10-31 14:30:50
strtotime()은 문자열을 추가하면 됩니다.
echo date('Y-m-d H:i:s',strtotime('2012-10-31 14:30:50 +1 second')); // 2012-10-31 14:30:51
echo date('Y-m-d H:i:s',strtotime('2012-10-31 14:30:50 +1 minute')); // 2012-10-31 14:31:50
echo date('Y-m-d H:i:s',strtotime('2012-10-31 14:30:50 +1 hour')); // 2012-10-31 15:30:50
echo date('Y-m-d H:i:s',strtotime('2012-10-31 14:30:50 +1 day')); // 2012-11-01 14:30:50
echo date('Y-m-d H:i:s',strtotime('2012-10-31 14:30:50 +1 month')); // 2012-12-01 14:30:50 ※
echo date('Y-m-d H:i:s',strtotime('2012-10-31 14:30:50 +1 year')); // 2013-10-31 14:30:50
echo date('Y-m-d H:i:s',strtotime('2012-10-31 14:30:50 +1 minute')); // 2012-10-31 14:31:50
echo date('Y-m-d H:i:s',strtotime('2012-10-31 14:30:50 +1 hour')); // 2012-10-31 15:30:50
echo date('Y-m-d H:i:s',strtotime('2012-10-31 14:30:50 +1 day')); // 2012-11-01 14:30:50
echo date('Y-m-d H:i:s',strtotime('2012-10-31 14:30:50 +1 month')); // 2012-12-01 14:30:50 ※
echo date('Y-m-d H:i:s',strtotime('2012-10-31 14:30:50 +1 year')); // 2013-10-31 14:30:50
기타 단순하게 초를 더하는 방법도 있습니다.
echo date('Y-m-d H:i:s',strtotime('2012-10-31 14:30:50') - 60*60*24); // 2012-10-30 14:30:50
echo date('Y-m-d H:i:s',strtotime('2012-10-31 14:30:50') + 60*60*24); // 2012-11-01 14:30:50
echo date('Y-m-d H:i:s',strtotime('2012-10-31 14:30:50') + 60*60*24); // 2012-11-01 14:30:50
그런데!
2012년 10월 31일에서 한 달을 더한 결과를 보세요. '2012-12-01'이 나오죠?
2012년 11월 31일은 없죠? 30일까지 있죠? 그렇게 해서 다음 달로 넘어갑니다.
day에 대한 설명 : The number of the day relevant to the end of the previous month. Values 1 to 28, 29, 30 or 31 (depending upon the month) reference the normal days in the relevant month. Values less than 1 (including negative values) reference the days in the previous month, so 0 is the last day of the previous month, -1 is the day before that, etc. Values greater than the number of days in the relevant month reference the appropriate day in the following month(s).
이 부분을 해결하려면? 직접 만들면 그만이죠.
sscanf('2012-10-31 14:30:50','%4d-%2d-%2d %2d:%2d:%2d',$y,$m,$d,$h,$i,$s);
// explode()를 이용해 자르든, preg_match()를 이용하든 각각의 값을 구합니다.
$m++; // $m+=1; $m=$m+1; … 1 증가의 다른 표현입니다. 10에서 11이 되었죠
// explode()를 이용해 자르든, preg_match()를 이용하든 각각의 값을 구합니다.
$m++; // $m+=1; $m=$m+1; … 1 증가의 다른 표현입니다. 10에서 11이 되었죠
11월의 일 수는 30입니다. 2월을 제외하고 나머진 고정이지요.
$last = array(1=>31,28,31,30,31,30,31,31,30,31,30,31); // 예) $last[2] = 28;
// 계산을 통해 $last[2]의 값 변경 (참고) 또는 아래처럼 간편하게 할당해도 됩니다.
$last = array(1=>31,date('t',strtotime('2012-02-01')),31,30,31,30,31,31,30,31,30,31); // 2012년은 2월 29일까지.
// 계산을 통해 $last[2]의 값 변경 (참고) 또는 아래처럼 간편하게 할당해도 됩니다.
$last = array(1=>31,date('t',strtotime('2012-02-01')),31,30,31,30,31,31,30,31,30,31); // 2012년은 2월 29일까지.
12월 + 1달 = 13월
10월 + 6달 = 16월
먼저 위와 같이 12월을 넘는 경우의 처리를 해줘야 합니다.
if ( $m>12 ) {
$y++;
$m-=12;
}
$y++;
$m-=12;
}
간편하죠?
예제에선 10월 + 1달 = 11월이니까 해당 없죠? 남은 것은 말일 계산.
if ( $d>$last[$m] ) $d = $last[$m];
$last[11]은 30이죠. $d(31)가 크니까 마지막 날짜로 할당.
$new = sprintf('%04d-%02d-%02d %02d:%02d:%02d',$y,$m,$d,$h,$i,$s);
최종적으로 구한 $new 값은 '2012-11-30 14:30:50'입니다. 깔끔하죠?
Second, Minute, Hour, Day까지 신경쓰면 코드가 좀 길어지겠죠?
위에선 더한 경우만 들었지만 빼는 경우도 약간 차이나고요.
기타 생략합니다. 차근차근 구현해보세요.
‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥‥
Copy&Paste만 좋아하는 사람들, 간편한 것을 좋아하는 사람들에게 유용한
DB 함수를 사용한 방법 덧붙입니다.
- http://www.mysqlkorea.co.kr/sub.html?mcode=manual&scode=01&m_no=21728&cat1=12&cat2=355&cat3=0&lang=k
- http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
SELECT DATE_ADD('2012-10-31',INTERVAL 1 MONTH);
→ 2012-11-30
→ 2012-11-30
PHP 코드 생략합니다.
함께 나눈 이야기
댓글댓글 남기기