将Unix时间戳转换为时区?

Nec*_*ro. 12 php unix timezone datetime timestamp

我的unix时间戳设置为+5,但我想将其转换为-5,EST标准时间.我只是在那个时区生成时间戳,但是我从其他来源抓取它,它将它放在+5.

当前未修改的时间戳被转换为日期

<? echo gmdate("F j, Y, g:i a", 1369490592) ?>
Run Code Online (Sandbox Code Playgroud)

Joh*_*nde 35

使用DateTimeDateTimeZone:

$dt = new DateTime('@1369490592');
$dt->setTimeZone(new DateTimeZone('America/Chicago'));
echo $dt->format('F j, Y, g:i a');
Run Code Online (Sandbox Code Playgroud)

  • 工作完美.谢谢=) (3认同)
  • @Stefan 这确实是错误的。Unix 时间戳始终为 UTC。一些有趣的代码示例:ini_set('date.timezone', 'America/Los_Angeles'); $现在=时间(); $utc = new DateTime("@{$now}", new DateTimeZone('美国/纽约')); echo "{$utc-&gt;format('c')}\n"; // 2014-08-27T15:24:09+00:00 $utc = new DateTime("@{$now}"); echo "{$utc-&gt;format('c')}\n"; // 2014-08-27T15:24:09+00:00 ini_set('date.timezone', 'UTC'); $utc = new DateTime("@{$now}", new DateTimeZone('UTC')); echo "{$utc-&gt;format('c')}\n"; // 2014-08-27T15:24:09+00:00 (2认同)