[英]Show closest result to current date & time (MySQL & PHP)
I'm trying to make a query but after hours of trying I can't seem to get it right. 我正在尝试进行查询,但是经过数小时的尝试,我似乎无法正确理解。 What I'm trying to do is showing 1 thing from a database that is closest to the currect date & time and after the current date & time.
我想做的是从数据库中显示最接近当前日期和时间并且在当前日期和时间之后的一件事。
my columns look like this: 我的专栏看起来像这样:
date time
1364399654 15:00
1364684400 16:00
1367272800 12:00
my PHP looks like this: 我的PHP看起来像这样:
$timestamp_now = strtotime(date('d-m-Y')); //Creates a timestamp of the currect date
$time_now = date('H:i');
$sql = mysql_query('SELECT * FROM table_name ORDER BY date ASC, time ASC (WHERE date > '.$timestamp_now.') AND (time > "'.$time_now.'") LIMIT 1') or die(mysql_error());
$data = mysql_fetch_array($sql);
However, this doesn't work. 但是,这不起作用。 Any words of advice?
有什么建议吗?
I would ditch using the PHP date/time methods and rely on MySQL giving a query that looks like 我会放弃使用PHP日期/时间方法,并依靠MySQL给出一个看起来像这样的查询
SELECT * FROM table_name
WHERE date > CURRENT_DATE
OR (
date = CURRENT_DATE
AND
time > CURRENT_TIME
)
ORDER BY date ASC, time ASC LIMIT 1
The OR makes sure that it gets the correct records else the TIME portion would block ie a result at 03:00 from the next day from appearing if the current time was at 06:00 OR会确保它获取正确的记录,否则TIME部分将被阻止,即如果当前时间为06:00,则从第二天开始的第二天的03:00出现结果
I see you are using timestamp values there so you can always still pass in the PHP date numeric in place of CURRENT_DATE. 我看到您在此处使用时间戳记值,因此您仍可以始终传递PHP日期数字来代替CURRENT_DATE。 This would give a final script of
这将给出最终脚本
$timestamp_now = strtotime(date('d-m-Y')); //Creates a timestamp of the currect date
$sql = mysql_query('SELECT * FROM table_name
WHERE date > '.$timestamp_now.'
OR (
date = '.$timestamp_now.'
AND
time > CURRENT_TIME
)
ORDER BY date ASC, time ASC LIMIT 1') or die(mysql_error());
$data = mysql_fetch_array($sql);
I would advise considering changing the database if possible to store just as a MySQL DATETIME field, as then you can change this query to simply WHERE datetime > NOW()
, but that's entirely up to you. 我建议考虑如果可能的话,将数据库更改为仅存储为MySQL DATETIME字段,因为这样您就可以将查询更改为
WHERE datetime > NOW()
,但这完全取决于您。 Just have always found MySQL date handling more logical than PHPs. 只是一直发现MySQL日期处理比PHP更逻辑。
You should use one more test : 您应该再进行一次测试:
DATE(date) > CURDATE()
: if it's tomorrow or later DATE(date) > CURDATE()
:如果是明天或更晚 DATE(date) = CURDATE() AND time > TIME(NOW())
: it it's today, but later DATE(date) = CURDATE() AND time > TIME(NOW())
:是今天,但是稍后 The full code : 完整代码:
$sql = mysql_query('SELECT *
FROM table_name
WHERE DATE(date) > CURDATE()
OR (DATE(date) = CURDATE() AND time > TIME(NOW()))
ORDER BY date ASC, time ASC
LIMIT 1'
) or die(mysql_error());
$data = mysql_fetch_array($sql);
Please note you're using timestamp in your date
field. 请注意,您在
date
字段中使用时间戳。 Timestamp include date and time, so maybe you'll be able to use this request : 时间戳记包括日期和时间,因此也许您可以使用此请求:
SELECT *
FROM table_name
WHERE date > NOW()
ORDER BY date ASC
LIMIT 1
我相信您需要在ORDER BY之类的结构之前使用WHERE构造SQL
SELECT * FROM table WHERE (date>$now) and (time>$now) ORDER BY date,time LIMIT 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.