newbie question --- date comparison

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply

Thu Mar 20, 2003 4:49 pm

I am new to this forum and programming in PHP in general. I am working on a website that will have a calendar of events and have the following question.
I have the code for the events calendar, however there is afeature that I would like to include and that is to list on the home page events that will take place in the next 7 days. So, I will query the calendarOfevents table and get the date of the events and see if it takes place within seven days from current date. Since I am new to PHP, I am not sure how to accomplish this. Looking at sample codes, I am able to open the database and read the event_date column on the table. I have become a little familiar with the IF statement, however, I don't know how to compare to a range of dates all at once. Any help will be appreciated very much.


Sun Mar 23, 2003 8:17 pm

for anybody that is interested here is the code to read events from your database and post events that take place within the next 7 days. The date of events, day, month and year are stored in separate columns in the database...

$flag="0"; #flag to set if any events within the next 7 days
$today=date("Y-m-d"); #sets today's date in required format
$from = strtotime($today); #convert it to unix time format
$db = mysql_connect("localhost","root","hello");
mysql_select_db("calendar",$db); #select database
$result = mysql_query("SELECT * FROM calendar_events",$db);
if ($myrow = mysql_fetch_array($result))
do {
$dateahead = mktime(0, 0, 0, date("m", $from), date("d", $from) + 7, date("Y", $from));#date 7 days ahead
if (($from<=$eventdate) && ($eventdate<=$dateahead)) #event within next 7 days?
$flag="1"; //set flag event meets criteria
echo " this event within the next 7 days <br>";
echo $myrow["event"]; #post event
while ($myrow = mysql_fetch_array($result));
echo "Lame! database is empty!";
if ($flag=="0")
{echo "Bummer! no events for the next 7 days!";}


That's alll!
If anybody has a better way to accomplish the above let me know

Post Reply