获取在特定日期之前添加的任何行

发布于 2024-10-31 11:08:09 字数 284 浏览 4 评论 0原文

这可能吗?我用它来将日期插入到名为“date”的字段中:

$date=date("m/d/y");
$sql="INSERT INTO pool (date) VALUES('$date' )";
$result=mysql_query($sql);

我用这个语句来获取一周前的日期:

$variable = date('d-m-y', strtotime('-1 week'));

那么我将如何选择上周添加的任何行?

Would this be possible? I've used this to insert the date into a field called "date":

$date=date("m/d/y");
$sql="INSERT INTO pool (date) VALUES('$date' )";
$result=mysql_query($sql);

I've used this statement to get the date a week ago:

$variable = date('d-m-y', strtotime('-1 week'));

So how would I SELECT any rows which were added last week?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛泛之交 2024-11-07 11:08:09

您不应将日期存储为 m/d/y,而应将其存储为 Ymd :

$date=date("Y-m-d");
$sql="INSERT INTO pool (date) VALUES('$date' )";

在数据库中,您的日期将类似于 2011-04-09

这种格式更容易使用:按字母顺序比较会起作用。

这意味着搜索早于某个日期的行将变成如下所示:

$variable = date('Y-m-d', strtotime('-1 week'));
$query = "select * from pool where date < '$variable'";

另请注意,您可以使用 DATE 列 - 这将允许与 MySQL 中的日期和时间函数

Instead of storing your dates as m/d/y, you should store them as Y-m-d :

$date=date("Y-m-d");
$sql="INSERT INTO pool (date) VALUES('$date' )";

In the database, you dates will then look like 2011-04-09.

That format is much easier to work with : alphabetical comparisons will work.

Which means that searching for rows that are older than a certain date would become something like this :

$variable = date('Y-m-d', strtotime('-1 week'));
$query = "select * from pool where date < '$variable'";

Also note that instead of working with a date field which is a varchar (or an equivalent) in your database, you could use a DATE column -- which would allow to to work with date and time functions in MySQL.

揽月 2024-11-07 11:08:09

如果日期字段是正确的日期类型,您可以执行 <或>在你的sql查询中。例如 -

SELECT * FROM table WHERE date > '$date'

如果您想要 1 周前到现在的所有内容,您可以执行上述操作或

SELECT * FROM table WHERE date BETWEEN '$date' AND NOW()

If the date field is a proper date type you can do < or > in your sql query. For example -

SELECT * FROM table WHERE date > '$date'

If you want everything from 1 week ago to now you can do something like the above or

SELECT * FROM table WHERE date BETWEEN '$date' AND NOW()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文