PHP에서 날짜와 시간을 다룰 때 가장 흔히 마주치는 문제 중 하나가 바로 타임존 미설정 문제입니다. 타임존을 제대로 설정하지 않으면 다음과 같은 문제들이 발생할 수 있습니다:
<?php
// 타임존이 설정되지 않은 상태
echo "현재 시간: " . date('Y-m-d H:i:s') . "\n";
echo "타임존: " . date_default_timezone_get() . "\n";
// Warning이 발생할 수 있음
$date = new DateTime();
echo $date->format('Y-m-d H:i:s T');
?>
; 한국 시간대로 설정
date.timezone = "Asia/Seoul"
; 또는 다른 시간대 예시
; date.timezone = "America/New_York"
; date.timezone = "Europe/London"
; date.timezone = "UTC"
<?php
// 방법 1: date_default_timezone_set() 사용
date_default_timezone_set('Asia/Seoul');
// 방법 2: ini_set() 사용
ini_set('date.timezone', 'Asia/Seoul');
// 설정 확인
echo "설정된 타임존: " . date_default_timezone_get() . "\n";
echo "현재 시간: " . date('Y-m-d H:i:s') . "\n";
?>
<?php
// 특정 타임존으로 DateTime 객체 생성
$timezone = new DateTimeZone('Asia/Seoul');
$date = new DateTime('now', $timezone);
echo "한국 시간: " . $date->format('Y-m-d H:i:s T') . "\n";
// 다른 타임존으로 변환
$date->setTimezone(new DateTimeZone('America/New_York'));
echo "뉴욕 시간: " . $date->format('Y-m-d H:i:s T') . "\n";
// UTC로 변환
$date->setTimezone(new DateTimeZone('UTC'));
echo "UTC 시간: " . $date->format('Y-m-d H:i:s T') . "\n";
?>
<?php
class TimezoneManager {
private $userTimezone;
public function __construct($userTimezone = 'Asia/Seoul') {
$this->userTimezone = $userTimezone;
}
public function getCurrentTime() {
$timezone = new DateTimeZone($this->userTimezone);
$date = new DateTime('now', $timezone);
return $date->format('Y-m-d H:i:s T');
}
public function convertToUserTime($utcTimestamp) {
$utc = new DateTimeZone('UTC');
$userTz = new DateTimeZone($this->userTimezone);
$date = new DateTime($utcTimestamp, $utc);
$date->setTimezone($userTz);
return $date->format('Y-m-d H:i:s T');
}
public function convertToUTC($localTime) {
$userTz = new DateTimeZone($this->userTimezone);
$utc = new DateTimeZone('UTC');
$date = new DateTime($localTime, $userTz);
$date->setTimezone($utc);
return $date->format('Y-m-d H:i:s');
}
}
// 사용 예시
$koreanUser = new TimezoneManager('Asia/Seoul');
$americanUser = new TimezoneManager('America/New_York');
echo "한국 사용자 시간: " . $koreanUser->getCurrentTime() . "\n";
echo "미국 사용자 시간: " . $americanUser->getCurrentTime() . "\n";
// UTC 시간을 사용자 시간대로 변환
$utcTime = '2024-01-15 12:00:00';
echo "UTC {$utcTime}는 한국 시간으로: " . $koreanUser->convertToUserTime($utcTime) . "\n";
?>
<?php
class DatabaseTimeHandler {
private $pdo;
public function __construct($pdo) {
$this->pdo = $pdo;
// 데이터베이스 타임존을 UTC로 설정
$this->pdo->exec("SET time_zone = '+00:00'");
}
public function saveCurrentTime($userId) {
// 현재 시간을 UTC로 저장
$utcTime = new DateTime('now', new DateTimeZone('UTC'));
$stmt = $this->pdo->prepare("
INSERT INTO user_activities (user_id, created_at)
VALUES (?, ?)
");
return $stmt->execute([
$userId,
$utcTime->format('Y-m-d H:i:s')
]);
}
public function getUserActivities($userId, $userTimezone = 'Asia/Seoul') {
$stmt = $this->pdo->prepare("
SELECT id, created_at
FROM user_activities
WHERE user_id = ?
");
$stmt->execute([$userId]);
$activities = $stmt->fetchAll(PDO::FETCH_ASSOC);
// UTC 시간을 사용자 타임존으로 변환
foreach ($activities as &$activity) {
$utc = new DateTime($activity['created_at'], new DateTimeZone('UTC'));
$utc->setTimezone(new DateTimeZone($userTimezone));
$activity['local_time'] = $utc->format('Y-m-d H:i:s T');
}
return $activities;
}
}
?>
<?php
class TimezoneUtils {
/**
* 사용 가능한 타임존 목록 반환
*/
public static function getAvailableTimezones() {
return DateTimeZone::listIdentifiers();
}
/**
* 주요 타임존 목록 반환
*/
public static function getCommonTimezones() {
return [
'UTC' => 'UTC',
'Asia/Seoul' => '한국 (KST)',
'Asia/Tokyo' => '일본 (JST)',
'Asia/Shanghai' => '중국 (CST)',
'America/New_York' => '미국 동부 (EST/EDT)',
'America/Los_Angeles' => '미국 서부 (PST/PDT)',
'Europe/London' => '영국 (GMT/BST)',
'Europe/Paris' => '프랑스 (CET/CEST)',
];
}
/**
* 타임존 유효성 검사
*/
public static function isValidTimezone($timezone) {
return in_array($timezone, DateTimeZone::listIdentifiers());
}
/**
* 시간대 간 시차 계산
*/
public static function getTimeDifference($timezone1, $timezone2) {
$tz1 = new DateTimeZone($timezone1);
$tz2 = new DateTimeZone($timezone2);
$date1 = new DateTime('now', $tz1);
$date2 = new DateTime('now', $tz2);
$offset1 = $tz1->getOffset($date1);
$offset2 = $tz2->getOffset($date2);
return ($offset1 - $offset2) / 3600; // 시간 단위로 반환
}
}
// 사용 예시
echo "한국과 뉴욕의 시차: " . TimezoneUtils::getTimeDifference('Asia/Seoul', 'America/New_York') . "시간\n";
if (TimezoneUtils::isValidTimezone('Asia/Seoul')) {
echo "유효한 타임존입니다.\n";
}
?>
<?php
// 애플리케이션 전역 타임존 설정
class AppConfig {
const DEFAULT_TIMEZONE = 'UTC';
const DISPLAY_TIMEZONE = 'Asia/Seoul';
public static function initializeTimezone() {
// 서버 기본 타임존을 UTC로 설정
date_default_timezone_set(self::DEFAULT_TIMEZONE);
}
}
// 애플리케이션 시작 시 호출
AppConfig::initializeTimezone();
?>
<?php
// 환경별 설정
$config = [
'development' => [
'timezone' => 'Asia/Seoul',
'display_timezone' => true
],
'production' => [
'timezone' => 'UTC',
'display_timezone' => false
]
];
$env = $_ENV['APP_ENV'] ?? 'development';
date_default_timezone_set($config[$env]['timezone']);
?>