views:

198

answers:

2

Hi Again, 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.

+1  A: 

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.

Chad Birch
A: 

You experiment with strtotime:

$sqldate = date('Y-m-d', strtotime('+2 days'));
christian studer
strtotime messes up, as i dont think thats how you implement it in the date function, i get 1969-12-31 no matter what the $page is
J Siegal
Hmm, it works for me. date takes two parameters: One formatting string and one optional timestamp. strtotime generates a time stamp from an almost arbitrary string. (Sometimes it feels like it's magic...)
christian studer
the problem im having is embedding a variable into strtotime, for example strtotime("+'$page' days") isn't having it
J Siegal
You've got too many '''s there. The correct syntax would be: strtotime("+$page days")(Or, if you want to be absolutely sure or use an array: strtotime("+{$page} days");)
christian studer