从数据库中选择6个月以上的项目?

时间:2021-05-05 11:15:38

I've got events in a database that have a Unix timestamp (ex: 1350450000).

我在数据库中有一个具有Unix时间戳的事件(例如:1350450000)。

$result = mysql_query("SELECT DISTINCT type FROM events");

I need to select the items that are newer than 6 months from the current date including anything scheduled after the current date. I've been messing around with several ways to do this, but no luck getting anything to work.

我需要选择从当前日期起6个月以内的项目,包括当前日期之后安排的任何项目。我一直在乱搞几种方法来做到这一点,但没有运气得到任何工作。

$result = mysql_query("SELECT DISTINCT type FROM events WHERE monthyear >= ...?");

I know I should avoid mysql_* functions, but the entire site has been coded with it and I'm just making the minor changes.

我知道我应该避免使用mysql_ *函数,但整个网站都已用它编码,我只是做了一些小改动。

2 个解决方案

#1


2  

 SELECT ... WHERE stampColumn > UNIX_TIMESTAMP() - (60 * 60 * 24 * 180)

#2


0  

You can use strtotime PHP function to get a timestamp representing 6 months back time and use it in the query:

您可以使用strtotime PHP函数来获取表示6个月后退时间的时间戳并在查询中使用它:

$ts = strtotime("-6 month");
$result = mysql_query("SELECT DISTINCT type FROM events WHERE monthyear >= {$ts}");

#1


2  

 SELECT ... WHERE stampColumn > UNIX_TIMESTAMP() - (60 * 60 * 24 * 180)

#2


0  

You can use strtotime PHP function to get a timestamp representing 6 months back time and use it in the query:

您可以使用strtotime PHP函数来获取表示6个月后退时间的时间戳并在查询中使用它:

$ts = strtotime("-6 month");
$result = mysql_query("SELECT DISTINCT type FROM events WHERE monthyear >= {$ts}");