简体   繁体   English

要求在 2 天内找出日期

[英]Request to find out the date in 2 days

How to write a sql query to find out that there are 2 days left before the current date.如何编写sql查询以找出距离当前日期还有 2 天。 In php, this can be done via:在 php 中,可以通过以下方式完成:

$res['end_date'] - time () < 86400 * 3;

How can I do the same after 1 sql query, well or better, only 2 days, if less so that it does not work out, well, if it works out, it's okay.在 1 个sql查询之后,我怎么能做同样的事情,好或更好,只有 2 天,如果更少以至于它不起作用,那么,如果它起作用,没关系。

UPD:升级版:

It is necessary to compose a sql query that will select only those records that have 2 days left before the end_date expires有必要编写一个sql查询,该查询将 select 仅在end_date到期前还有2天的那些记录

The type is int for the field end_date and is stored via the time () function in php .字段end_date的类型是int ,并通过time () function 存储在php中。

Can't compose a WHERE clause.无法编写WHERE子句。

You can use the FROM_UNIXTIME function to convert it to a DateTime you can then use the NOW() plus 2 days to check if the date is under 2 days.您可以使用FROM_UNIXTIME function 将其转换为 DateTime 然后您可以使用NOW()加上 2 天来检查日期是否小于 2 天。 You then have to check that the date is before the current time otherwise you'll get dates that have already gone.然后,您必须检查日期是否早于当前时间,否则您将获得已经过去的日期。

SELECT
    end_date
FROM 
    table
WHERE 
    FROM_UNIXTIME(end_date) <= NOW() + INTERVAL 2 DAY
AND
    FROM_UNIXTIME(end_date) > NOW()

Assuming that you are storing an epoch timestamp (the number of seconds since January 1st, 1970), I would recommend:假设您正在存储一个纪元时间戳(自 1970 年 1 月 1 日以来的秒数),我建议:

select *
from mytable
where end_date >= unix_timestamp() and end_date < unix_timestamp() + 2 * 24 * 60 * 60

unix_timestamp() gives you the current epoch. unix_timestamp()为您提供当前纪元。 You can use simple math to add two days to that.您可以使用简单的数学来增加两天。

The upside of this approach is that this does direct filtering against the store value, so this can take advantagae of an index on end_date - as opposed to converting the timestamp to a date, which requires converting the whole column before the filtering can happen.这种方法的好处是,它会直接过滤存储值,因此这可以利用end_date上的索引 - 而不是将时间戳转换为日期,这需要在过滤发生之前转换整个列。 So this is much more efficient.所以这样效率更高。

You can ajust the inequalities as you prefer.您可以根据自己的喜好调整不等式。 I used a half-open interval (inclusive on the lower bound and exclusive on the upper bound), which is a widely used approach.我使用了一个半开区间(包括下限和不包括上限),这是一种广泛使用的方法。

I ended up doing this:我最终这样做了:

$time = time();
$params = $db->query("SELECT * FROM `params` WHERE (`end_date` - {$time}) < 86400 * 3");

And it worked.它奏效了。

I always do我经常做

select *
from mytable
where FROM_UNIXTIME(end_date) < NOW() + INTERVAL 2 DAY

This will get results where two days in the future is ahead of the end date ie, anything that will end within 2 days (or has already ended as I didn't add a check for that)这将在未来两天提前结束日期的情况下得到结果,即任何将在 2 天内结束的结果(或者已经结束,因为我没有添加检查)

Edit: I see you can't use where If you cannot use where clause编辑:我看到你不能使用 where 如果你不能使用 where 子句

select FROM_UNIXTIME(end_date) - INTERVAL 2 DAY as end_date 
from mytable

And then check in php if the result is before or after.然后检查 php 的结果是之前还是之后。 This will show all results however然而,这将显示所有结果

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

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