We help IT Professionals succeed at work.

PHP Calendar with events

katlees
katlees asked
on
Medium Priority
310 Views
Last Modified: 2012-06-27
I have the following code where it writes out a calendar and I can do next, previous etc.
I am having trouble getting the dates to populate on the correct dates.

How it is now, all events show up on today's date...


<?php
 
$nextm=$_GET['month'];
$nexty=$_GET['year'];
 

require_once("includes/db_login.inc");
 
if($nextm == "13")
{
        $nextm=1;
        $nexty++;
}
 
if($nextm == "0")
{
        $nextm=12;
        $nexty--;
}
 
//This gets today's date
$date =time () ;
 
//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
 
If ($nextm != ""){
        $month=$nextm;
        
}
else{
$month = date('m', $date) ;
 
}
$month2  = date('m', $date);
$minmonth=$month-1;
$maxmonth=$month+1;
If ($nexty != "") {
        $year=$nexty;
}
else{
$year = date('Y', $date) ;
}
$year2 = date('Y', $date) ;
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
 
//This gets us the month name
$title = date('F', $first_day) ; 
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
 
//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
 
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ; 
//Here we start building the table heads
echo "<table  border=2 class=table width=100% height=80%>";
echo "<tr><th class=head colspan=7> <a href=calendar2.php?month=$minmonth&year=$year><<</a>   $title $year <a href=calendar2.php?month=$maxmonth&year=$year>>></a>   </th></tr>";
echo "<tr class=body><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>";
 
//This counts the days in the week, up to 7
$day_count = 1;
 
echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 )
{
echo "<td> </td>";
$blank = $blank-1;
$day_count++;
} 
//sets the first day of the month to 1
$day_num = 1;
 
//count up the days, untill we've done all of them in the month
 
//while ($row = mysql_fetch_array($res))
//{
// while ($row2 = mysql_fetch_array($res2))
// {    
	while ( $day_num <= $days_in_month )
	{
      	if ($day==$day_num and $year2==$year and $month2==$month) // so this is today
		{
                
            ?><td class=day2><? echo "$day_num <br />";
 
 
        	$resl = "select * from Events";
    		$res=mysql_query($resl) or die(mysql_error());
		$row = mysql_fetch_array($res);
        	
		$resl2 = "select * from Events WHERE StartDate = $day_num"; 
     		$res2=mysql_query($resl2) or die(mysql_error());
		$row2 = mysql_fetch_array($res2);
  while ($row = mysql_fetch_assoc($res))
      
	    {
                
  $event_id = $row['ID'];
                $event_name = $row['Event'];
                $event_date = $row['StartDate'];
	       		$event_time = mysql2timestamp($event_date);
                $event_display_date = date("D, F j, Y", $event_time);
                $event_description = $row['LocationID'];
                $curDate = getdate($event_time);
                $curMonth = $curDate['mon'];
                $curYear = $curDate['year'];
                $curFullMonth = $curDate['month'];
 
 echo "$event_name\n";
 }
		?> 
		
		
		</td><?
		
		}
        	else
		{
 
        	$resl = "select * from Events  WHERE StartDate = $day_num";
    		$res=mysql_query($resl) or die(mysql_error());
		$row = mysql_fetch_array($res);
        	
		$resl2 = "select * from Events WHERE StartDate = $day_num"; 
     		$res2=mysql_query($resl2) or die(mysql_error());
		$row2 = mysql_fetch_array($res2);
                
?><td class=day> <? echo "$day_num";?> </td><?
        
        }
$day_num++;
$day_count++;
 
//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
} 
//    }
//    }
    
//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 )
{
echo "<td>  </td>";
$day_count++;
}
 
echo "</tr></table>"; 

 

 ?>

Open in new window

Comment
Watch Question

this is where jquery comes in handy.


http://jqueryui.com/demos/datepicker/


:)


br
Can you post the mysql2timestamp function ?
I don't think you need to check if each day is the current one. But check this code:

<?php
 
$nextm=$_GET['month'];
$nexty=$_GET['year'];
 

require_once("includes/db_login.inc");
 
if($nextm == "13")
{
        $nextm=1;
        $nexty++;
}
 
if($nextm == "0")
{
        $nextm=12;
        $nexty--;
}
 
//This gets today's date
$date =time () ;
 
//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
 
If ($nextm != ""){
        $month=$nextm;
        
}
else{
$month = date('m', $date) ;
 
}
$month2  = date('m', $date);
$minmonth=$month-1;
$maxmonth=$month+1;
If ($nexty != "") {
        $year=$nexty;
}
else{
$year = date('Y', $date) ;
}
$year2 = date('Y', $date) ;
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
 
//This gets us the month name
$title = date('F', $first_day) ; 
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
 
//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
 
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ; 
//Here we start building the table heads
echo "<table  border=2 class=table width=100% height=80%>";
echo "<tr><th class=head colspan=7> <a href=calendar2.php?month=$minmonth&year=$year><<</a>   $title $year <a href=calendar2.php?month=$maxmonth&year=$year>>></a>   </th></tr>";
echo "<tr class=body><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>";
 
//This counts the days in the week, up to 7
$day_count = 1;
 
echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 )
{
echo "<td> </td>";
$blank = $blank-1;
$day_count++;
} 
//sets the first day of the month to 1
$day_num = 1;
 
//count up the days, untill we've done all of them in the month
 
//while ($row = mysql_fetch_array($res))
//{
// while ($row2 = mysql_fetch_array($res2))
// {    
	while ( $day_num <= $days_in_month )
	{
      	if ($day==$day_num and $year2==$year and $month2==$month) // so this is today
		{
                
            ?><td class=day2><? echo "$day_num <br />";
 
 
        	$resl = "select * from Events";
    		$res=mysql_query($resl) or die(mysql_error());
		$row = mysql_fetch_array($res);
        	
		$resl2 = "select * from Events WHERE StartDate = $day_num"; 
     		$res2=mysql_query($resl2) or die(mysql_error());
		$row2 = mysql_fetch_array($res2);
  while ($row = mysql_fetch_assoc($res))
      
	    {
                
  $event_id = $row['ID'];
                $event_name = $row['Event'];
                $event_date = $row['StartDate'];
	       		$event_time = mysql2timestamp($event_date);
                $event_display_date = date("D, F j, Y", $event_time);
                $event_description = $row['LocationID'];
                $curDate = getdate($event_time);
                $curMonth = $curDate['mon'];
                $curYear = $curDate['year'];
                $curFullMonth = $curDate['month'];
 
 echo "$event_name\n";
 }
		?> 
		
		
		</td><?
		
		}
        	else
		{
        
        $thisday = mktime(0,0,0,$month, $day_num, $year);
        $dd = date ("Y-m-d",$thisday); 
       	
		$resl2 = "select * from Events WHERE SUBSTR(StartDate FROM 1 FOR 10) = '$dd'"; 
     	$res2=mysql_query($resl2) or die(mysql_error());
		$row2 = mysql_fetch_array($res2);
		
		$event_name2 = $row2['Event'];
	    //$event_time2 = mysql2timestamp($event_date2);
        //$event_display_date2 = date("D, F j, Y", $event_time2);
                
?><td class=day><? echo "$day_num\n"; echo "$event_name2\n ";?> </td><?
        
        }
$day_num++;
$day_count++;
 
//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
} 
//    }
//    }
    
//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 )
{
echo "<td>  </td>";
$day_count++;
}
 
echo "</tr></table>"; 

 

 ?>

Open in new window

Most Valuable Expert 2011
Author of the Year 2014

Commented:
Some information on DATETIME processing is available here.  One of the examples includes some design patterns for a calendar.  HTH, ~Ray
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Author

Commented:
Roads_Roads - yours works good except:
1) It shows all the events on todays date
2) It doesn't show multiple events on a day - only the first one.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Make a Google search for this string, "Example of PHP and MySQL Web Calendar" and also look at the jQuery date picker scripts.  There are many good examples in addition to the principles illustrated in the article linked here.
From line 137 to 143 - switch to this for the multiple events shown on the day

<td class=day>
<? echo "$day_num\n";

while($row2 = mysql_fetch_array($res2)){
   $event_name2 = $row2['Event'];
}            

?> </td>

Author

Commented:
are you sure your row numbers are right Roads?
ok, so from 129 (the "else " part regarding the days that are different that this day) should be

else{
 $thisday = mktime(0,0,0,$month, $day_num, $year);
 $dd = date ("Y-m-d",$thisday); 
       	
 $resl2 = "select * from Events WHERE SUBSTR(StartDate FROM 1 FOR 10) = '$dd'"; 
 $res2=mysql_query($resl2) or die(mysql_error());
 ?>
 <td class=day>
 <? echo "$day_num\n"; 
 while($row2 = mysql_fetch_array($res2)){
    $event_name2 = $row2['Event'];
 }            
?> 
</td>
<?
}

Open in new window

Author

Commented:
Maybe I"m missing something, but when I do that, I get all the events looped on today's date.. no other events show up... My whole code is below...
Each day should loop through the events on that day...
<?php
 
$nextm=$_GET['month'];
$nexty=$_GET['year'];
 

require_once("includes/db_login.inc");
 
if($nextm == "13")
{
        $nextm=1;
        $nexty++;
}
 
if($nextm == "0")
{
        $nextm=12;
        $nexty--;
}
 
//This gets today's date
$date =time () ;
 
//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
 
If ($nextm != ""){
        $month=$nextm;
        
}
else{
$month = date('m', $date) ;
 
}
$month2  = date('m', $date);
$minmonth=$month-1;
$maxmonth=$month+1;
If ($nexty != "") {
        $year=$nexty;
}
else{
$year = date('Y', $date) ;
}
$year2 = date('Y', $date) ;
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
 
//This gets us the month name
$title = date('F', $first_day) ; 
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
 
//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
 
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ; 
//Here we start building the table heads
echo "<table  border=2 class=table width=100% height=80%>";
echo "<tr><th class=head colspan=7> <a href=calendar2.php?month=$minmonth&year=$year><<</a>   $title $year <a href=calendar2.php?month=$maxmonth&year=$year>>></a>   </th></tr>";
echo "<tr class=body><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>";
 
//This counts the days in the week, up to 7
$day_count = 1;
 
echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 )
{
echo "<td> </td>";
$blank = $blank-1;
$day_count++;
} 
//sets the first day of the month to 1
$day_num = 1;
 
//count up the days, untill we've done all of them in the month
 
//while ($row = mysql_fetch_array($res))
//{
// while ($row2 = mysql_fetch_array($res2))
// {    
	while ( $day_num <= $days_in_month )
	{
      	if ($day==$day_num and $year2==$year and $month2==$month) // so this is today
		{
                
            ?><td class=day2 bgcolor="#FF0000"><? echo "$day_num <br />$event_name";
 
 
        	$resl = "select * from Events";
    		$res=mysql_query($resl) or die(mysql_error());
		$row = mysql_fetch_array($res);
        	
		$resl2 = "select * from Events WHERE StartDate = $day_num"; 
     		$res2=mysql_query($resl2) or die(mysql_error());
		$row2 = mysql_fetch_array($res2);
  while ($row = mysql_fetch_assoc($res))
      
	    {
                
  $event_id = $row['ID'];
                $event_name = $row['Event'];
                $event_date = $row['StartDate'];
	       		$event_time = mysql2timestamp($event_date);
                $event_display_date = date("D, F j, Y", $event_time);
                $event_description = $row['LocationID'];
                $curDate = getdate($event_time);
                $curMonth = $curDate['mon'];
                $curYear = $curDate['year'];
                $curFullMonth = $curDate['month'];
 
 echo "$event_name\n";
 }
		?> 
		
		
		</td>


		<?
		
		}
       else{
 $thisday = mktime(0,0,0,$month, $day_num, $year);
 $dd = date ("Y-m-d",$thisday); 
       	
 $resl2 = "select * from Events WHERE SUBSTR(StartDate FROM 1 FOR 10) = '$dd'"; 
 $res2=mysql_query($resl2) or die(mysql_error());
 ?>
 <td class=day>
 <? echo "$day_num\n"; 
 while($row2 = mysql_fetch_array($res2)){
    $event_name2 = $row2['Event'];
 }            
?> 
</td>
<?
} 

$day_num++;
$day_count++;
 
//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
} 
//    }
//    }
    
//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 )
{
echo "<td>  </td>";
$day_count++;
}
 
echo "</tr></table>"; 

 

 ?>

Open in new window

Does this make any change ?

<?php
 
$nextm=$_GET['month'];
$nexty=$_GET['year'];
 

require_once("includes/db_login.inc");
 
if($nextm == "13")
{
        $nextm=1;
        $nexty++;
}
 
if($nextm == "0")
{
        $nextm=12;
        $nexty--;
}
 
//This gets today's date
$date =time () ;
 
//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
 
If ($nextm != ""){
        $month=$nextm;
        
}
else{
$month = date('m', $date) ;
 
}
$month2  = date('m', $date);
$minmonth=$month-1;
$maxmonth=$month+1;
If ($nexty != "") {
        $year=$nexty;
}
else{
$year = date('Y', $date) ;
}
$year2 = date('Y', $date) ;
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
 
//This gets us the month name
$title = date('F', $first_day) ; 
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
 
//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
 
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ; 
//Here we start building the table heads
echo "<table  border=2 class=table width=100% height=80%>";
echo "<tr><th class=head colspan=7> <a href=calendar2.php?month=$minmonth&year=$year><<</a>   $title $year <a href=calendar2.php?month=$maxmonth&year=$year>>></a>   </th></tr>";
echo "<tr class=body><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>";
 
//This counts the days in the week, up to 7
$day_count = 1;
 
echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 )
{
echo "<td> </td>";
$blank = $blank-1;
$day_count++;
} 
//sets the first day of the month to 1
$day_num = 1;
 
//count up the days, untill we've done all of them in the month
 
//while ($row = mysql_fetch_array($res))
//{
// while ($row2 = mysql_fetch_array($res2))
// {    
	while ( $day_num <= $days_in_month )
	{
      	if ($day==$day_num and $year2==$year and $month2==$month) // so this is today
		{
                
            ?><td class=day2 bgcolor="#FF0000"><? echo "$day_num <br />";
 
 
        	$resl = "select * from Events";
    		$res=mysql_query($resl) or die(mysql_error());
		$row = mysql_fetch_array($res);
        	
		$resl2 = "select * from Events WHERE StartDate = $day_num"; 
     		$res2=mysql_query($resl2) or die(mysql_error());
		$row2 = mysql_fetch_array($res2);
  while ($row = mysql_fetch_assoc($res))
      
	    {
                
  $event_id = $row['ID'];
                $event_name = $row['Event'];
                $event_date = $row['StartDate'];
	       		$event_time = mysql2timestamp($event_date);
                $event_display_date = date("D, F j, Y", $event_time);
                $event_description = $row['LocationID'];
                $curDate = getdate($event_time);
                $curMonth = $curDate['mon'];
                $curYear = $curDate['year'];
                $curFullMonth = $curDate['month'];
 
 echo "$event_name\n";
 }
		?> 
		
		
		</td>


		<?
		
		}
       else{
 $thisday = mktime(0,0,0,$month, $day_num, $year);
 $dd = date ("Y-m-d",$thisday); 
       	
 $resl2 = "select * from Events WHERE SUBSTR(StartDate FROM 1 FOR 10) = '$dd'"; 
 $res2=mysql_query($resl2) or die(mysql_error());
 ?>
 <td class=day>
 <? echo "$day_num\n"; 
 while($row2 = mysql_fetch_array($res2)){
    $event_name2 = $row2['Event'];
    echo"$event_name2<br />";
 }            
?> 
</td>
<?
} 

$day_num++;
$day_count++;
 
//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
} 
//    }
//    }
    
//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 )
{
echo "<td>  </td>";
$day_count++;
}
 
echo "</tr></table>"; 

 

 ?>

Open in new window

Author

Commented:
It puts the events on their date but puts ALL events on today's date
OK, give me a sec.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
That looks like it worked.... Yeah! Thanks.
Thank you. This code requires some cleaning up anyway :P

Author

Commented:
Roads - can you help with one more thing - the events are coming off all days before today... how do I keep the calendar populated?

Author

Commented:
nevermind... I was wrong.
Sure ? Feel free to post - I'll try to help.

Author

Commented:
Roads_roads - I posted another question to turn this calendar to a weekly view instead of monthly.. can you help? I dont' want to start over with the code.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_26987449.html
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.