PHP 中的 IST 日期格式未更改 UTC 日期

UTC date not changing in IST date format in PHP

基于 REST API 页面 documentation 日期采用 UTC 格式 yyyy-MM-dd’T’HH:mm:ss (Eg: 2017-01-02T08:12:53) 当我点击 API 时,我得到的日期是 1619693307000。使用 PHP 中的 strtotime() 转换了此日期。 在 PHP.

中将此日期转换为 d-m-Y h:i:s IST 的正确方法是什么

我用这段代码做了同样的事情。

<?php echo date("Y-m-d h:i:s", '1619693307000') ?> //OUTPUT: 53296-01-14 01:00:00 

上面的输出是绝对错误的。 混淆是正确地将 UTC 转换为 IST 区域,我应该怎么做才能看到正确的输出作为 PHP 中的日期。我阅读了这个 Whosebug 和 Google 上的所有线程。 但都没有帮助。

请帮忙...

您的时间戳位数过多,因此可能以毫秒为单位。这似乎是一件常见的 javascript 事情。所以除以 1000,我强烈建议使用 DateTime objects/interfaces 而不是旧式 strtotime()/date()/etc 函数。

$millis = 1619693307000;
$seconds = $millis / 1000;

$t = new DateTime('', new DateTimezone('Asia/Kolkata'));
$t->setTimestamp($seconds);

var_dump(
    $t->format("Y-m-d h:i:s T")
);

输出:

string(23) "2021-04-29 04:18:27 IST"

此外,“UTC”不是一种格式,它是一个时区。 2017-01-02T08:12:53 是 ISO8601 格式。它还有一个方便的格式快捷方式:

var_dump(
    $t->format("c")
);

输出:

string(25) "2021-04-29T16:18:27+05:30"

编辑:不同时区和格式:

$t = new DateTime('', new DateTimezone('UTC'));
$t->setTimestamp($seconds);

var_dump(
    $t->format("Y-m-d\Th:i:s")
);

输出:

string(19) "2021-04-29T10:48:27"