简体   繁体   English

WHERE DATE_FORMAT = DATE_FORMAT不正确

[英]WHERE DATE_FORMAT = DATE_FORMAT is inaccurate

I am storing time using a unix timestamp integer for each booking in my database. 我在数据库中为每个预订使用unix时间戳整数存储时间。 I am having a problem where my query is not selecting things properly, and seems to be a day off. 我遇到了一个问题,即我的查询未正确选择内容,似乎一天休息了。

For example, on my website I have a calendar table which shows each day of the month. 例如,在我的网站上,我有一个日历表,其中显示了每月的每一天。 I use the following code in order to populate products for each day: 我使用以下代码来填充每天的产品:

    $sql = "select b.*, p.name as p_name, p.color as p_color
            from `bookings` as b
            left join `products` as p on (p.id = b.id_product)
            where date_format(from_unixtime(b.timestamp), '%M %e %Y') = date_format(from_unixtime(:timestamp), '%M %e %Y')
            order by b.timestamp asc";
    $stm = $this->app->db->prepare($sql);
    $stm->bindParam(':timestamp', $this->timestamp);
    $stm->execute();
    $res = $stm->fetchAll();

Now, it is showing bookings I made for January 24th on the 25th day in my calendar. 现在,它显示了我日历中第25天的1月24日的预订。

The problem seems to lie with this part of the query: 问题似乎在于查询的这一部分:

            where date_format(from_unixtime(b.timestamp), '%M %e %Y') = date_format(from_unixtime(:timestamp), '%M %e %Y')

For example, when $this->timestamp = '1453698000' (the 25th) and b.timestamp = '1453618800' (the 24th), it is showing this record on the 25th in my calendar. 例如,当$this->timestamp = '1453698000' (第25位)和b.timestamp = '1453618800' (第24位)时,它将在我的日历的第25位显示此记录。

I have absolutely no idea why this is happening. 我完全不知道为什么会这样。 I've tried changing the date_format query to use '%d-%m-%Y', I've tried adding '00:00:00', I've tried manually setting the timezone in the PDO construct, I've made sure all my time zones are lining up correctly (which they are) by echoing the timestamps using the PHP date() function. 我尝试将date_format查询更改为使用'%d-%m-%Y',尝试添加'00:00:00',尝试在PDO构造中手动设置时区,通过使用PHP date()函数回显时间戳,确保我的所有时区都正确对齐(它们是正确的)。

Why is this happening? 为什么会这样呢? Any help would be greatly appreciated. 任何帮助将不胜感激。

First off, you can debug this more thoroughly by running: 首先,您可以运行以下命令来更彻底地调试它:

SELECT date_format(from_unixtime(1453698000), '%M %e %Y')

and

SELECT date_format(from_unixtime(1453618800), '%M %e %Y')

and seeing what comes out of each. 并查看每个结果。 This should reveal the problem. 这应该可以揭示问题。

I don't know how you thought you set the timezone when passing in a unix timestamp, but I don't think this is possible. 我不知道您在传递unix时间戳时如何设置时区,但我认为这是不可能的。 The timezone is used to calculate the local time and date from the generic timestamp. 时区用于根据通用时间戳计算本地时间和日期。

It doesn't matter what timezone PHP thinks each timestamp is in, which is why your PHP date() functions match up. PHP认为每个时间戳都位于哪个时区并不重要,这就是为什么PHP date()函数匹配的原因。 What matters is the timezone your MySQL engine is in as this is where you are doing the conversion. 重要的是您的MySQL引擎所在的时区,因为这是您进行转换的地方。

I would calculate the exact timestamp from the correct datetime in your application (eg 25th at 00:00:00) and pass it in to the query. 我会从您的应用程序中的正确日期时间(例如25:00:00)计算出确切的时间戳,并将其传递给查询。

The conversion itself: 转换本身:

 where date_format(from_unixtime(b.timestamp), '%M %e %Y') = date_format(from_unixtime(:timestamp), '%M %e %Y')

invalidates any index on the timestamp, so may lead to slow performance. 使时间戳上的任何索引无效,因此可能导致性能降低。

It would be better to write this as: 最好这样写:

 WHERE b.timestamp >= :timestamp
   AND b.timestamp <  :timestamp + INTERVAL 1 DAY

Your other options is to change the timezone of mysql . 您的其他选择是更改mysql时区

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

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