1

所以我页面上的日期格式显示提前 6 小时的时间。我该如何更改它以显示正确的时间这是我的查询

   $query = "SELECT firstName,lastName, post,date_format(postDate,'%M,%d,%Y at %H:%i') as     mydatefield
      FROM users INNER JOIN posts ON userId = empl_Id
      ORDER BY postDate";

这会将值插入表中

   $query = "INSERT INTO posts(postId,empl_Id,post,postDate)
        VALUES('','$empId','$idea',NOW())";
    $result = mysqli_query($db, $query);
4

2 回答 2

1

大概您正在以 UTC 存储时间,并且您比 UTC 晚 6 小时。

不要更改您的查询。时区表示是应用程序的视图逻辑的关注点,而不是控制器的关注点。

每次在页面中显示日期/时间时,只需将 TZ 偏移量添加到 UTC 值即可。您可以在此处查看示例:http ://www.php.net/manual/en/book.datetime.php

于 2013-03-26T21:57:58.117 回答
0

本问题所述,您可以按照此处所述进行操作。

或者您可以选择,如下面的答案所示,这样做:

您可以使用 DateTime::setTimezone()。如果您的 UTC 日期是 UNIX 时间戳,则可以使用如下代码:

$date = new DateTime();
$date->setTimezone(new DateTimeZone('UTC'));
$date->setTimestamp(1297869844); // this should be the 'current' then 
$date->setTimezone(new DateTimeZone('Europe/Paris'));

echo $date->format('Y-m-d H:i:s');
// Will print 2011-02-16 16:24:04

然后使用它将值插入到您的数据库中。更改表示(毕竟它是 unix 时间戳,您甚至可能想要存储它,它只是被翻译成人类形式作为一种说话方式)

于 2013-03-26T22:13:06.570 回答