在mysqli_query中,是否可以比较日期(“Y-m-d H:i:s”)和时间()?

时间:2022-05-11 21:28:31

I would like to select from database 10 most popular posts posted in last 24 hours based on likes. In database for each post I stored time when the post was posted using date("Y-m-d H:i:s") in column post_date.

我想从数据库中选择最受欢迎的10个帖子。在数据库中,当post在post_date中使用日期(“y -m- dh: I:s”)时,我存储的每个帖子的时间。

I need something like this:

我需要这样的东西:

$most_popular = mysqli_query($mysqli_connect, "SELECT * FROM posts WHERE (time() - post_date) < 86400 ORDER BY likes DESC LIMIT 10");

4 个解决方案

#1


2  

you can do following:

你可以做以下:

SELECT * 
FROM posts 
WHERE post_date > DATE_SUB(NOW(), INTERVAL 1 DAY) 
ORDER BY likes DESC 
LIMIT 10

#2


2  

I would just compare the post_date with 24 hours before:

我只是比较一下post_date和24小时之前:

$last_24_hours = date("Y-m-d H:i:s", strtotime("-24 Hours"));
$query = sprintf("SELECT * 
                    FROM posts 
                   WHERE post_date > '%s'
                ORDER BY likes DESC
                   LIMIT 10", $last_24_hours);
$most_popular = mysqli_query($mysqli_connect, $query);

#3


0  

Your question is: "Is it possible to compare date (“Y-m-d H:i:s”) with time() inside mysqli_query?"

您的问题是:“是否有可能在mysqli_query中比较日期(“Y-m-d H:i:s”)和时间()?”

The answer is yes:

答案是肯定的:

SELECT TIME('2003-12-31 01:02:03'); // returns '01:02:03'

NOTE: This CAN'T handle many dates, if that's your question.

注意:如果这是你的问题的话,这不能解决很多约会。

#4


0  

SELECT *
FROM posts
WHERE post_date >= (now() - INTERVAL 1 DAY)
ORDER BY ...
etc...

#1


2  

you can do following:

你可以做以下:

SELECT * 
FROM posts 
WHERE post_date > DATE_SUB(NOW(), INTERVAL 1 DAY) 
ORDER BY likes DESC 
LIMIT 10

#2


2  

I would just compare the post_date with 24 hours before:

我只是比较一下post_date和24小时之前:

$last_24_hours = date("Y-m-d H:i:s", strtotime("-24 Hours"));
$query = sprintf("SELECT * 
                    FROM posts 
                   WHERE post_date > '%s'
                ORDER BY likes DESC
                   LIMIT 10", $last_24_hours);
$most_popular = mysqli_query($mysqli_connect, $query);

#3


0  

Your question is: "Is it possible to compare date (“Y-m-d H:i:s”) with time() inside mysqli_query?"

您的问题是:“是否有可能在mysqli_query中比较日期(“Y-m-d H:i:s”)和时间()?”

The answer is yes:

答案是肯定的:

SELECT TIME('2003-12-31 01:02:03'); // returns '01:02:03'

NOTE: This CAN'T handle many dates, if that's your question.

注意:如果这是你的问题的话,这不能解决很多约会。

#4


0  

SELECT *
FROM posts
WHERE post_date >= (now() - INTERVAL 1 DAY)
ORDER BY ...
etc...