1. 우선 입력할때는 codeigniter date helper의 now() 함수를 사용합니다.
사용하기 위해서 config.php를 수정해줘야 gmt 시간을 사용할 수 있습니다.
$config['time_reference'] = 'gmt';
//기본값이 local로 되어 있습니다.
//제 경우엔 autoload에 선언
$this->load->helper('date');
$time = now(); //위의 time_referense 가 gmt 이므로 gmt timestamp를 반환합니다.
2. gmt시간을 local로 바꿔줄때는 현재 타임존을 입력해줘야 하는데 이 부분이 수동이라 자바스크립트의
getTimezoneOffset() 함수를 이용하여 gmt와 현재 클라이언트PC 시간의 차이를 가져옵니다.
(제 경우엔 사이트 전체에서 사용해야 해서 hook에 구현을 했습니다만 일반적인 경우엔 그냥 선언해서 쓰시면 됩니다)
//hook으로 구현한 내용입니다. 꼼수. ^^
ob_start();
?>
<script language="javascript" type="text/javascript">
now = new Date();
localtime = now.getTimezoneOffset();
[removed](localtime);
</script>
<?php
$g = ob_get_contents();
ob_end_clean();
define('GMT_DIF', $g);
실제로 변환하는 함수입니다.
/**
* gmt timestamp 를 local timestamp 변환
*
* @author Jongwon Byun <codeigniterk@gmail.com>
* @param string $time : gmt timestamp
* @param string $daylight_saving : 섬머타임
* @return string $local_timestamp : local 기준으로 변환된 timestamp
*/
function gmt_2_local($time, $daylight_saving='false')
{
//hook에서 선언한 gmt와 local의 시간차이에 따른 타임존 구하기
switch (GMT_DIFF)
{
case ('720'):
$time_zone = 'UM12';
break;
case ('660'):
$time_zone = 'UM11';
break;
case ('600'):
$time_zone = 'UM10';
break;
case ('540'):
$time_zone = 'UM9';
break;
case ('480'):
$time_zone = 'UM8';
break;
case ('420'):
$time_zone = 'UM7';
break;
case ('360'):
$time_zone = 'UM6';
break;
case ('300'):
$time_zone = 'UM5';
break;
case ('240'):
$time_zone = 'UM4';
break;
case ('210'):
$time_zone = 'UM35';
break;
case ('180'):
$time_zone = 'UM3';
break;
case ('120'):
$time_zone = 'UM2';
break;
case ('60'):
$time_zone = 'UM1';
break;
case ('0'):
$time_zone = 'UTC';
break;
case ('-60'):
$time_zone = 'UP1';
break;
case ('-120'):
$time_zone = 'UP2';
break;
case ('-180'):
$time_zone = 'UP3';
break;
case ('-210'):
$time_zone = 'UP35';
break;
case ('-240'):
$time_zone = 'UP4';
break;
case ('-270'):
$time_zone = 'UP45';
break;
case ('-300'):
$time_zone = 'UP5';
break;
case ('-330'):
$time_zone = 'UP55';
break;
case ('-360'):
$time_zone = 'UP6';
break;
case ('-420'):
$time_zone = 'UP7';
break;
case ('-480'):
$time_zone = 'UP8';
break;
case ('-540'):
$time_zone = 'UP9';
break;
case ('-570'):
$time_zone = 'UP95';
break;
case ('-600'):
$time_zone = 'UP10';
break;
case ('-660'):
$time_zone = 'UP11';
break;
case ('-720'):
$time_zone = 'UP12';
break;
}
$local_timestamp = gmt_to_local($time, $time_zone, $daylight_saving);
return $local_timestamp;
}
사용법은 다음과 같습니다.
$local_timestamp = gmt_2_local($gmt_timestamp);
작업하다가 덤으로 매뉴얼의 오타도 발견했습니다.
date helper Timezone Reference에 UM25 -> UM35 가 맞습니다.
저런 식으로 뒷자리가 2자리로 되어 있는 값들은 전부 +1을 해줘야 합니다.
UP45 -> UP55
|