【问题标题】:How to convert local time to UTC format?如何将本地时间转换为 UTC 格式?
【发布时间】:2011-06-25 08:42:12
【问题描述】:

我有约会

$timeZome = timezone_open('Europe/Kiev');
$date = new DateTime();
$date->setTimezone($timeZome);
$date->setDate(2011, 06,25);
$date->setTime(11,35,00);

如何呈现?

20110625T040000Z

【问题讨论】:

  • 这可能是国际性的事情,但你日期的“T040000Z”部分代表什么?
  • "T040000Z" 只是一个例子
  • 所以它的时间戳? T 和 Z 代表什么?

标签: php datetime utc


【解决方案1】:

这会做你想做的事:

$date = new DateTime();
$date->setTimezone(new DateTimeZone('Europe/Kiev'));
$date->setDate(2011, 06,25);
$date->setTime(11,35,00);
$date->setTimezone(new DateTimeZone('UTC'));

echo $date->format('Ymd\THis\Z'); // format string to match question

非常简单:设置日期/时间/区域,修改时区和打印格式。

【讨论】:

    【解决方案2】:

    你可能只需要在你的日期对象上调用format,我想。

    echo $date->format('Ymd'); // 20110625
    

    更新: 您可能想研究在日期类中使用the existing constants,或者如果您的需求与提供的日期不同,只需扩展它并添加一个方法来解析并返回您想要的日期格式。

    【讨论】:

    • 我可以转换日期,但我不明白如何转换UTC格式的时间。
    猜你喜欢
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多