Php 是否将Unix时间戳转换为时区?
我有一个unix时间戳设置为+5,但我想将其转换为-5,EST标准时间。我只想在那个时区生成时间戳,但我从另一个来源获取它,它的值是+5 正在转换为日期的当前未修改的时间戳Php 是否将Unix时间戳转换为时区?,php,unix,datetime,timezone,timestamp,Php,Unix,Datetime,Timezone,Timestamp,我有一个unix时间戳设置为+5,但我想将其转换为-5,EST标准时间。我只想在那个时区生成时间戳,但我从另一个来源获取它,它的值是+5 正在转换为日期的当前未修改的时间戳 <? echo gmdate("F j, Y, g:i a", 1369490592) ?> 使用和: 由于的编辑队列已满,我将添加更详细的答案 从 $timezone参数和当前时区在 $time参数可以是UNIX时间戳(例如@946684800) 这就是为什么在从unix时间戳创建DateTime对象时,应
<? echo gmdate("F j, Y, g:i a", 1369490592) ?>
使用和:
由于的编辑队列已满,我将添加更详细的答案
从
$timezone参数和当前时区在
$time参数可以是UNIX时间戳(例如@946684800)
这就是为什么在从unix时间戳创建DateTime
对象时,应该始终指定时区(即使是默认时区)的主要原因。请参见以下受启发的解释代码:
一种更简单的方法是: 使用
gmdate()
时,将您的时区(以秒为单位)添加到gmdate中的unix_戳记中
假设我的时区是GMT+5:30。因此,5小时30分钟(以秒计)将是19800
因此,我将这样做:gmdate(“fj,Y,g:ia”,1369490592+19800)
这里有一个函数可以将unix/gmt/utc时间戳转换为您可能感兴趣的所需时区
function unix_to_local($timestamp, $timezone){
// Create datetime object with desired timezone
$local_timezone = new DateTimeZone($timezone);
$date_time = new DateTime('now', $local_timezone);
$offset = $date_time->format('P'); // + 05:00
// Convert offset to number of hours
$offset = explode(':', $offset);
if($offset[1] == 00){ $offset2 = ''; }
if($offset[1] == 30){ $offset2 = .5; }
if($offset[1] == 45){ $offset2 = .75; }
$hours = $offset[0].$offset2 + 0;
// Convert hours to seconds
$seconds = $hours * 3600;
// Add/Subtract number of seconds from given unix/gmt/utc timestamp
$result = floor( $timestamp + $seconds );
return $result;
}
如果我错了请纠正我@John这肯定不行吗?在创建DateTime对象的实例时,此方法将使用未指定的systems(PHP配置)默认时区。喜欢因此,当转换到美国/芝加哥时,它将根据系统时区进行更改,假设它与UNIX时间戳本身的+5不同,他试图将其更改约10个区域?@Stefan这确实是错误的。Unix时间戳始终为UTC。一些有趣的代码示例:ini_set('date.timezone','America/Los_Angeles')$现在=时间()$utc=新的日期时间(“@{$now}”,新的日期时区(“美国/纽约”);回显“{$utc->format('c')}\n”;//2014-08-27T15:24:09+00:00$utc=新日期时间(“@{$now}”);回显“{$utc->format('c')}\n”;//2014-08-27T15:24:09+00:00初始设置(“日期、时区”、“UTC”)$utc=新的日期时间(“@{$now}”,新的日期时区('utc');回显“{$utc->format('c')}\n”;//2014-08-27T15:24:09+00:00我想提出一些建议:如果UNIX时间戳是一个变量,则必须使用双引号使其如下工作:`new DateTime(@$timestamp”);如果使用单引号或不使用引号,则会出现错误。
$dt = new DateTime('@1369490592');
// use your default timezone to work correctly with unix timestamps
// and in line with other parts of your application
date_default_timezone_set ('America/Chicago'); // somewhere on bootstrapping time
…
$dt->setTimeZone(new DateTimeZone(date_default_timezone_get()));
// set timezone to convert time to the other timezone
$dt->setTimeZone(new DateTimeZone('America/Chicago'));
echo $dt->format('F j, Y, g:i a');
function unix_to_local($timestamp, $timezone){
// Create datetime object with desired timezone
$local_timezone = new DateTimeZone($timezone);
$date_time = new DateTime('now', $local_timezone);
$offset = $date_time->format('P'); // + 05:00
// Convert offset to number of hours
$offset = explode(':', $offset);
if($offset[1] == 00){ $offset2 = ''; }
if($offset[1] == 30){ $offset2 = .5; }
if($offset[1] == 45){ $offset2 = .75; }
$hours = $offset[0].$offset2 + 0;
// Convert hours to seconds
$seconds = $hours * 3600;
// Add/Subtract number of seconds from given unix/gmt/utc timestamp
$result = floor( $timestamp + $seconds );
return $result;
}