Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

newbie question --- date comparison

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

newbie question --- date comparison

Postby masterc » 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.
Dank
masterc
New php-forum User
New php-forum User
 
Posts: 5
Joined: Mon Mar 17, 2003 3:17 pm

code

Postby masterc » 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...

<?php
$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 {
$darray=array($myrow["year"],$myrow["month"],$myrow["day"]);
$evntdate=implode("-",$darray);
$eventdate=strtotime($evntdate);
$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));
}
else
{
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
masterc
New php-forum User
New php-forum User
 
Posts: 5
Joined: Mon Mar 17, 2003 3:17 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.