更改我的SQL查询的小时间隔

时间:2021-10-13 10:38:59

So my date format on my page is showing the time 6 hours ahead. How can i change it so it shows the correct time this is my query

所以我的页面上的日期格式显示了提前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";

This inserts values into table

这会将值插入表中

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

2 个解决方案

#1


1  

Presumably you're storing times in UTC and you're located 6 hours behind UTC.

据推测,您将以UTC格式存储时间,并且距离UTC仅6小时。

Do not change your query. Timezone presentation is a concern of your application's View logic, not the controller.

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

Every time you display a date/time in your page just add the TZ offset to the UTC value. You can see examples here: http://www.php.net/manual/en/book.datetime.php

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

#2


0  

As noted in this question, you can either do it as described here.

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

Or you might choose, as the answer below that states, do it this way :

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

you can use DateTime::setTimezone(). If your UTC date is an UNIX timestamp, you can use some code like this :

你可以使用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

And then use that to insert the values into your database. Changing the representation (after all its the unix timestamp, you might even want store that, which just gets translated to a human form as a way of speaking)

然后使用它将值插入数据库。更改表示(在它的所有unix时间戳之后,您甚至可能想要存储,只是将其转换为人类形式作为一种说话方式)

#1


1  

Presumably you're storing times in UTC and you're located 6 hours behind UTC.

据推测,您将以UTC格式存储时间,并且距离UTC仅6小时。

Do not change your query. Timezone presentation is a concern of your application's View logic, not the controller.

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

Every time you display a date/time in your page just add the TZ offset to the UTC value. You can see examples here: http://www.php.net/manual/en/book.datetime.php

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

#2


0  

As noted in this question, you can either do it as described here.

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

Or you might choose, as the answer below that states, do it this way :

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

you can use DateTime::setTimezone(). If your UTC date is an UNIX timestamp, you can use some code like this :

你可以使用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

And then use that to insert the values into your database. Changing the representation (after all its the unix timestamp, you might even want store that, which just gets translated to a human form as a way of speaking)

然后使用它将值插入数据库。更改表示(在它的所有unix时间戳之后,您甚至可能想要存储,只是将其转换为人类形式作为一种说话方式)