본문으로 건너뛰기

"timezone" 태그로 연결된 4개 게시물개의 게시물이 있습니다.

모든 태그 보기

Carbon 으로 timestamp 파싱하기

· 약 1분

Carbon으로 타임스탬프를 파싱하는 데에는 createFromTimestamp 메소드가 있다.

Carbon::createFromTimestamp(1576249805)->format();

하지만 더 쉽게 parse 메소드를 사용해 파싱할 수도 있다.

$timestamp = 1576249805;
Carbon::parse('@' . $timestamp)->format();

여기서의 @는 오류를 무시하는 기분이 들어서 찾아보았는데 표준이였다.

Example #2 DateTime::setTimestamp() alternative in PHP 5.2

$ts = 1171502725;
$date = new DateTime("@$ts");
echo $date->format('U = Y-m-d H:i:s') . "\n";
?>

참조

타임존 데이터 최신버전으로 업데이트하기

· 약 2분

타임존 데이터는 php의 버전을 따라 올라가는데, 실무에선 항상 최신버전을 사용하기 쉽지 않다. 그럴 때 데이터만 업데이트하는 방법을 쓸 수 있다.

2019.06 현재 최신 타임존 데이터베이스는 2019a (2019.01) 버전이다.

perl

perl로 설치되는 일반적인 경우는 아래와 같이 쉽게 설치 가능하다.

# perl 로 설치
$ perl install timezonedb

# 끝!

phpize

그렇지 않은 경우 라이브러리를 수동으로 빌드해줘야한다.

# 타임존 데이터 다운로드
$ curl -LO https://pecl.php.net/get/timezonedb-2019.1.tgz

# 압축 해제
$ tar -xvzf timezonedb-2019.1.tgz
$ cd timezonedb-2019.1

# 빌드
$ phpize
$ ./configure --with-php-config=${PHP_CONFIG_PATH}
$ make && make install

# 라이브러리를 extensions 폴더로 이동
$ mv timezonedb.so /usr/local/php/extentions

# 라이브러리 추가
$ vi php.ini
$ extension=timezonedb.so

# 아파치 재시작
$ apachectl restart

# 버전 확인
$ /usr/bin/php -r "echo timezone_version_get();"
2019.01

참조

Centos7 Timezone 변경하기

· 약 1분
# 기존 설정 백업
$ mv /etc/localtime /etc/localtime.bak

# 타임존 연결
$ ln -s /usr/share/zoneinfo/Asia/Seoul /etc/localtime

# 백업을 안하고 바로 연결시
$ ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime

timedatectl

timedatectl 명령어를 사용해 쉽게 바꿀 수 있다.

# 한줄로 깔끔하게
$ timedatectl set-timezone Asia/Seoul

여담

시간날 때 세팅용 쉘을 만들어야겠다.

Ubuntu에서 timezone 변경

· 약 2분

Ubuntu 설치시 다른 국가로 되어있는 시간대를 바꿔야한다.

$ tzselect

명령어를 실행하면 아래처럼 번호를 선택할 수 있다.

Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
1) Africa
2) Americas
3) Antarctica
4) Asia
5) Atlantic Ocean
6) Australia
7) Europe
8) Indian Ocean
9) Pacific Ocean
10) coord - I want to use geographical coordinates.
11) TZ - I want to specify the time zone using the Posix TZ format.
#? 4

4번 아시아를 선택하면 국가 선택지가 나온다.

Please select a country whose clocks agree with yours.
1) Afghanistan 18) Israel 35) Palestine
2) Armenia 19) Japan 36) Philippines
3) Azerbaijan 20) Jordan 37) Qatar
4) Bahrain 21) Kazakhstan 38) Russia
5) Bangladesh 22) Korea (North) 39) Saudi Arabia
6) Bhutan 23) Korea (South) 40) Singapore
7) Brunei 24) Kuwait 41) Sri Lanka
8) Cambodia 25) Kyrgyzstan 42) Syria
9) China 26) Laos 43) Taiwan
10) Cyprus 27) Lebanon 44) Tajikistan
11) East Timor 28) Macau 45) Thailand
12) Georgia 29) Malaysia 46) Turkmenistan
13) Hong Kong 30) Mongolia 47) United Arab Emirates
14) India 31) Myanmar (Burma) 48) Uzbekistan
15) Indonesia 32) Nepal 49) Vietnam
16) Iran 33) Oman 50) Yemen
17) Iraq 34) Pakistan
#? 23

국가를 선택 후 마지막으로 확인을 해주면

The following information has been given:

Korea (South)

Therefore TZ=\'Asia/Seoul\' will be used.
Local time is now: Tue Feb 14 23:04:10 KST 2017.
Universal Time is now: Tue Feb 14 14:04:10 UTC 2017.
Is the above information OK?
1) Yes
2) No
#? 1

...
Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Seoul

성공적으로 반영되었다.

확인

$ date
Tue Feb 14 23:04:21 KST 2017