PHP 시간대 설정

PHP 시간대 설정
PHP 시간대 최초 설정
It is not safe to rely on the system's timezone settings.
timezone 미입력시 경고 메시지

1 문제상황[ | ]

test.php에서 phpinfo();를 실행시켰더니 다음과 같은 경고 메시지가 나온다.

Warning: phpinfo(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Seoul' for 'KST/9.0/no DST' instead in /opt/apache2/htdocs/test.php on line 1

[root@zetawiki ~]# php -r "echo date('Y-m-d H:i:s').PHP_EOL;"
PHP Warning:  date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Asia/Seoul' for 'KST/9.0/no DST' instead in Command line code on line 1
2014-09-07 07:29:53

2 확인[ | ]

php.ini를 열어 확인해보자.

확인 예시
[root@zetawiki ~]# cat /etc/php.ini | grep timezone
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone =

→ date.timezone이 설정되지 않았다.

php.ini 파일이 여러 개 있을 수도 있다. phpinfo(); 결과에서 php.ini로 검색해보면 실제 사용되는 php.ini 파일을 찾을 수 있다.

3 수정[ | ]

3.1 방법 1: sed로 수정[ | ]

[root@zetawiki ~]# grep date.timezone /etc/php.ini
; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
;date.timezone =
[root@zetawiki ~]# cp /etc/php.ini /etc/php.ini.old
[root@zetawiki ~]# sed -i 's/;date.timezone =/date.timezone = Asia\/Seoul/g' /etc/php.ini
[root@zetawiki ~]# diff /etc/php.ini.old /etc/php.ini
946c946
< ;date.timezone =
---
> date.timezone = Asia/Seoul

3.2 방법 2: 직접 편집[ | ]

php.ini를 수정하자.

vi /etc/php.ini
변경 전
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone =

주석처리(;)를 제거하고 Asia/Seoul 값을 넣는다.

변경 후
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Seoul

4 확인[ | ]

[root@zetawiki ~]# php -r "echo date('Y-m-d H:i:s').PHP_EOL;"
2014-09-07 07:30:41
→ Warning이 나오지 않음 (정상)

5 httpd 재시작[ | ]

  • 웹페이지에도 적용하려면 httpd를 재시작해야 한다.
[root@zetawiki ~]# service httpd restart
httpd 를 정지 중:                                          [  OK  ]
httpd (을)를 시작 중:                                      [  OK  ]

6 최종 확인[ | ]

브라우저로 test.php를 열어보니, 이제 경고 없이 정상이다.

7 같이 보기[ | ]

8 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}