简体   繁体   English

10天前的SQL SELECT日期失败

[英]SQL SELECT date from 10 days ago failed

I'm havig some trouble with SQL: 我在使用SQL时遇到了一些麻烦:

First, I insert the current date, using PHP-time(): 首先,我使用PHP-time()插入当前日期:

mysql_query("
   INSERT INTO applicants (
      date_last_applied
   )
   VALUES (
      " . time() . "
   )
");

This works just fine. 这样很好。 However, I only want to execute an action when that ^ was inserted 10 days ago: 但是,我只想在10天前插入^时执行操作:

$result = mysql_query("
   SELECT * FROM applicants WHERE date_last_applied < DATE_SUB(NOW(), INTERVAL 10 DAY)
");
while ($row = mysql_fetch_array($result)) 
    echo $row['id_member'];

Although it should work (IMO), it doesn't. 尽管它应该可以工作(IMO),但不能。 D: d:

For possible solutions: it's not an option to use an other timeformat, because I'm using this format on my entire site. 对于可能的解决方案:不能选择使用其他时间格式,因为我在整个网站上都使用这种格式。

我认为将date_sub resutl转换为unixtime将解决您的问题。

date_last_applied < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 10 DAY))

while inserting rows you are using time (time()) of the local machine. 在插入行时,您正在使用本地计算机的时间(time())。 But while retrieving rows you are using the mysql server's time (NOW()). 但是,在检索行时,您正在使用mysql服务器的时间(NOW())。 Are you sure they match exactly and will continue to do so. 您确定它们完全匹配,并且会继续匹配。 You should try to use mysql server time everywhere. 您应该尝试在所有地方使用mysql服务器时间。

Also you are inserting rows in applicants and retrieving from DDFS_applicants. 另外,您正在申请人中插入行并从DDFS_applicants中检索。 May be that is the problem here 可能就是这里的问题

Since you are using a timestamp, and I'm guessing that you don't use a Data time field, Converting should solve your problem. 由于您使用的是时间戳记,而且我猜您没有使用数据时间字段,因此转换应该可以解决您的问题。 But there is a but! 但是有一个但是!

The UNIX_TIMESTAMP is always in GMT, so it could be that your PHP time() isn't the same as your UNIX_TIMESTAMP. UNIX_TIMESTAMP始终处于GMT中,因此可能是您的PHP time()与UNIX_TIMESTAMP不同。 If you find out its the same, then you should be good! 如果您发现其相同,那么您应该很好!

date_last_applied < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 10 DAY))

Ok, thanks for your help, y'all. 好的,谢谢您的帮助。 This code works flawlessly: 这段代码可以完美地工作:

 //get member's info
$result = mysql_query("
   SELECT * FROM DDFS_applicants 
   WHERE FROM_UNIXTIME(date_last_applied, '%Y %D %M') = FROM_UNIXTIME(UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY)), '%Y %D %M')
");
while ($row = mysql_fetch_array($result)) {
    echo $row['id_member'];

(So it works even to check if the date is the exact same date. If you want, you can even add hours to it, but I chose not to.) (因此,它甚至可以检查日期是否完全相同。如果需要,甚至可以增加几个小时,但我选择不这样做。)

Source: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-sub 资料来源: http : //dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-sub

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

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