Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

You experiment with strtotime:

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

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top