PHP 日期相关分页

发布于 2024-07-13 18:46:59 字数 398 浏览 6 评论 0原文

我有一个网站,在 sql 数据库中存储一堆记录,我想根据日期将其拉到页面上,该日期在 sql 中存储为 Ymd。 但是,我想根据日期对结果进行分页。

因此,对于索引,我想显示当天的所有结果,为此我将使用 php date() 函数作为查询中的 WHERE。 但我在分页时遇到了障碍。 我想在底部有一个按钮,可以通过 get 进入下一页,所以 index.php?page=2 就是明天,但我不知道如何从 WHERE 的数据库中可靠地选择“明天”。

看,我打算使用 date("U") 来获取第一页上第一天的 UNIX 时间(以秒为单位),然后添加 3600*$_GET['page'] 来增加下一页上的日期,但这似乎是一种草率的做法,最终可能会把我搞砸。 这是唯一的方法还是有更好、更实用的解决方案 - 谢谢很多人,我很感激。

I have a site that stores a bunch of records in an sql database that i want to pull onto a page based on the date, which is stored as Y-m-d in sql. But, I want to paginate the results based on day.

So for the index, i want to show all the results from the current day, for which i was going to use the php date() function as the WHERE in my QUERY. But I'm hitting a snag on doing the pagination. I want to have buttons at the bottom that go to the next page with a get, so index.php?page=2 would be tomorrow, but i cant figure out how to select "tomorrow" reliably from the database in my WHERE.

See, i was going to use date("U") to get the unix time in seconds of the first day on the first page and then just add 3600*$_GET['page'] for incrementing the date on the next pages, but that seems like a sloppy way to do it that might wind up messing me up. Is this the only way or is there a better, more practical solution - thanks a lot guys I appreciate it.

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

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

发布评论

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

评论(3

牛↙奶布丁 2024-07-20 18:47:00

如果第 2 页是明天,那么您将看到类似这样的内容:

$days_ahead = $page - 1;
$query = "... WHERE date = DATE(NOW()) + INTERVAL $days_ahead DAY ...";

请注意,这在第一页上也可以正常工作(假设 $page 默认为 1),它会在今天的日期上添加 0 天。

If page 2 is tomorrow, then you're going to be looking at something like this:

$days_ahead = $page - 1;
$query = "... WHERE date = DATE(NOW()) + INTERVAL $days_ahead DAY ...";

Note that this would work fine on the first page too (assuming $page gets defaulted to 1), it'd add 0 days to today's date.

若沐 2024-07-20 18:47:00

您尝试使用 strtotime:

$sqldate = date('Y-m-d', strtotime('+2 days'));

You experiment with strtotime:

$sqldate = date('Y-m-d', strtotime('+2 days'));
过期以后 2024-07-20 18:47:00

这就是我设法为我的网站修复它的方法

//'page' is a GET variable from url
if($page<=1) {
  $datemax = time();
  $datemin = time() - (1 * 2592000); //2592000 being seconds in a month
}
else{
  $datemax = time() - (($page - 1) * 2592000);
  $datemin = time() - ($page * 2592000);
}

然后显然查询将类似于

SELECT * FROM posts WHERE dateposted >=$datemin AND dateposted <=$datemax

This is how I managed to fix it for my site

//'page' is a GET variable from url
if($page<=1) {
  $datemax = time();
  $datemin = time() - (1 * 2592000); //2592000 being seconds in a month
}
else{
  $datemax = time() - (($page - 1) * 2592000);
  $datemin = time() - ($page * 2592000);
}

And then obviously the query will look something like

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