如何在php中获取带时区的日期/时间

时间:2014-02-20 21:04:06

标签: php

我正在尝试使用php以这种格式获取日期,但我找不到正确的格式。

2014-02-20T12:47:39.444-0700

我目前的代码:

 $MTtime = new DateTime('now', new DateTimeZone('America/Denver'));
 $UTCtime = new DateTime('now', new DateTimeZone('UTC'));
 $MTtime->format('Y-m-d H:i:s');
 $UTCtime->format('Y-m-d H:i:s');

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

尝试使用gmDate:

gmDate("Y-m-d\TH:i:s.u-Z");

http://www.php.net/gmdate

答案 1 :(得分:0)

感谢@DaOgre指出我的ATOM时间格式。

这是我最终如何使用它:

$UTCtime = new DateTime('now', new DateTimeZone('UTC'));
$data['logDateTime'] = $UTCtime->format(DateTime::ATOM);