Page 1 of 1

easy question about displaying mysql records

Posted: Wed Apr 18, 2007 3:58 pm
by jcsickz
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have recently found out how to display today's records from mysql, but now I want to display the records for the next 7 days as well as records from the next 30 days.

Here is my current code:

Code: Select all

<?php
$today = date("Y-m-d");
echo "Event Listing for " . $today
?>

<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("jcsickz_mc", $con);


$result = mysql_query("SELECT * FROM events WHERE date = '" . $today ."' ORDER BY date DESC");

echo "<table border='1'>
<tr>
<th>Date</th>
<th>Time</th>
<th>Venue</th>
<th>Description</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['date'] . "</td>";
  echo "<td>" . $row['time'] . "</td>";
  echo "<td>" . $row['venue'] . "</td>";
  echo "<td>" . $row['description'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>
how can I modify this code to display the next 7 days and the next 30 days?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Apr 19, 2007 1:09 am
by Christopher
Take a look at the time() function to create a timestamp of a date in the future. You can then pass that timestamp to the date() function to get a date string that you can use in your SQL.

http://www.php.net/manual/en/function.time.php

Once you have today's date and a future date, you can change you SQL to something like this:

Code: Select all

SELECT * FROM events WHERE date>='" . $today ."' AND date<='" . $future_date ."'