PHP 日期相关分页
我有一个网站,在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果第 2 页是明天,那么您将看到类似这样的内容:
请注意,这在第一页上也可以正常工作(假设 $page 默认为 1),它会在今天的日期上添加 0 天。
If page 2 is tomorrow, then you're going to be looking at something like this:
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.
您尝试使用 strtotime:
You experiment with strtotime:
这就是我设法为我的网站修复它的方法
然后显然查询将类似于
This is how I managed to fix it for my site
And then obviously the query will look something like