【问题标题】:php new dateTime and gmdate [duplicate]php new dateTime 和 gmdate [重复]
【发布时间】:2013-05-15 22:55:04
【问题描述】:

我将如何转换以下内容以使其使用datetime class?:

array(
    'post_date' => date('Y-m-d H:i:s', $date),
    'post_date_gmt' => gmdate('Y-m-d H:i:s', $date),
);

例如,这部分很简单,但 gmdate 怎么样?

$oDate = new DateTime($date);
array(
    'post_date' => $oDate->format('Y-m-d H:i:s'),
    'post_date_gmt' => gmdate('Y-m-d H:i:s', $date),
);

【问题讨论】:

  • 您只需将DateTimeZone 实例作为第二个参数传递给DateTime$date = new DateTime($date, new DateTimeZone('GMT'));
  • @OfirBaruch 太好了,我将关闭/删除这个问题
  • 大声笑我必须投票才能结束我自己的问题

标签: php


【解决方案1】:

试试这个:

$oDate->setTimezone(new DateTimeZone('GMT'));
$oDate->format('Y-m-d H:i:s');

【讨论】:

  • 别忘了这样做后他想回到标准时区。
【解决方案2】:

在这种情况下,GMT 需要两个对象一个。

$oDate = new DateTime($date);
$gmDate = new DateTime($date, new DateTimeZone('GMT'));

array(
   'post_date'     => $oDate->format('Y-m-d H:i:s'),
   'post_date_gmt' => $gmDate->format('Y-m-d H:i:s'),
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 2011-05-07
    • 1970-01-01
    相关资源
    最近更新 更多