简体   繁体   English

MySQL查询“多久前” + 6小时

[英]MySQL Query “How long ago” + 6hours

With the help of a friend, I got a webpage going that tracks different stats and saves it in an SQL database. 在一个朋友的帮助下,我得到了一个网页,该网页可以跟踪不同的统计信息并将其保存在SQL数据库中。

One of the information that returns, is when the latest score was submitted to the database. 返回的信息之一是最新分数何时提交到数据库。 It works fine, but the webhost is in a different timezone and I am unable to change that timezone. 它可以正常工作,但是Web主机位于不同的时区,并且我无法更改该时区。

So therefore I was thinking about changing our query to one which returns how long ago the score was added. 因此,因此,我正在考虑将查询更改为一个返回返回分数添加时间的查询。

Current code: 当前代码:

$statement = $adapter->query("
    select name, 
        SUM(score_1) as score_1, 
        SUM(score_2) as score_2, 
        SUM(score_3) as score_3,
        (SUM(score_1)+SUM(score_2)+SUM(score_3)) as total, 
        DATE_FORMAT(MAX(creation_time), '%d %b %H:%i') as creation_time 
    from score_entry 
    WHERE DATE(creation_time) = CURDATE() 
    group by name ORDER BY total DESC");

It grabs the information stored in the past day (from 00:00 this day), and I'm not sure if that is also affected by the incorrect timezone. 它获取过去一天(从今天00:00开始)中存储的信息,我不确定那是否也受不正确的时区影响。

After a lot of searching around, I can't seem to find the solution to my exact problem. 经过大量搜索后,我似乎无法找到确切问题的解决方案。

I have tried to set the timezone in MySQL, but it's a shared host by Namecheap , they don't allow it. 我试图在MySQL中设置时区,但Namecheap其作为共享主机,但他们不允许这样做。

Take a look at the time zone documentation . 查看时区文档

Using the SET time_zone = timezone; 使用SET time_zone = timezone; command you will be able to set the time zone on a per-connection basis. 命令,您将能够基于每个连接设置时区。

In addition, storing dates in a TIMESTAMP column makes MySQL convert the time to UTC and then it converts it back to the current time zone when you access it. 另外,将日期存储在TIMESTAMP列中会使MySQL将时间转换为UTC,然后在您访问时将其转换回当前时区。 Thus it makes storing and retrieving time zone agnostic. 因此,它使存储和检索时区不可知。

Set the time zone in your PHP script using the posted solution. 使用发布的解决方案在您的PHP脚本中设置时区。 It's also possible to send it the datetime to use in your query using PHP's date function. 也可以使用PHP的date函数将日期时间发送给查询中使用的日期时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM