Help with Making a PHP calendar!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dauzy
Forum Newbie
Posts: 11
Joined: Wed Jul 16, 2008 4:38 pm

Help with Making a PHP calendar!

Post by dauzy »

Hello,

I've been pulling my hair out trying to figure out how to do this....Hopefully someone can be of help =)...



Scenario:

I used this tutorial on making a calendar in PHP:
http://video.about.com/php/How-to-Creat ... lendar.htm

So, I need to make a PHP calendar....Easy enough right...but there one problem.

I made the "full" calendar but I need to modify the calendar to skip Sundays... This means that all Sundays are excluded when making table data.


So... the calendar would look like this:

July

Monday Tuesday Wednesday Thursday Friday Saturday

1 2 3 4 5
7 8 9 10 11 12
14 15 16 17 18 19
21 22 23 24 25 26
27 28 29 30 31

(sorry diagram isn't showing correctly...but the 1st is on Tues and so forth...)

Heres the php code that takes care of making the calendar....

Code: Select all

 
//Create a varible directory, which holds information that is taken from the scan() funtion.
   $directory = scan();
   //Variables for the day, month and year
   $date = time ();
   $day = date('d', $date);
   $month = date('m', $date);
   $year = date('Y', $date);
 
   //Finds the first day of the month
   $first_day = mktime(0,0,0,$month, 1, $year);
 
   //Calculates the day of the week that the first day is on
   $day_of_week = date('D', $first_day);
 
   //Sets the the day to a numerical value... Sun = 0...Sat = 6
   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;
   }
 
   //Calculates the days in the month
   $days_in_month = cal_days_in_month(0, $month, $year);
 
 
   $day_count = 1;
 
   //This while loop creates the blank spaces before the first day of the month.
   while($blank > 0)
   {
      echo "<td></td>";
      $blank = $blank-1;
      $day_count++;
   }
 
   $day_num = 1;
 
   //This while loop is used to create a table row and table ddta.
   while($day_num <= $days_in_month)
   {
      //If the $day_num is included in $directory new table data with corresponded .jpg will be used.
      if(in_array($day_num, $directory))
      {
         $event = "images/_calendar/July/calendar/".$day_num.".jpg";
         echo '<td width="145px" height="145px" background="'.$event.'">'.$day_num.'</td>';
         $day_num++;
         $day_count++;   
      }
      else
      {
         //If $day_num is not included in $directory new table data will be displayed with the default.jpg
         echo '<td width="145px" height="145px" background="images/_calendar/July/calendar/default.jpg">'.$day_num.'</td>';
         $day_num++;
         $day_count++;
      }
 
      //This will ensure table row in the calendar will only have 7 days to it.
      if($day_count > 7)
      {
      echo "</tr><tr>";
      $day_count = 1;
      }
   }
   
    //This will ensure table row in the calendar will only have 7 days to it.
   while($day_count > 1 && $day_count <= 7)
   {
      echo "<td></td>";
      $day_count++;
   }
 
Thanks,
Dauzy
Last edited by dauzy on Wed Jul 16, 2008 5:50 pm, edited 2 times in total.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Help with Making a PHP calendar!

Post by omniuni »

Try modifying your last two little loops to be for 6 instead of 7. That should remove the extra column, that being Sunday.

Calendars can be tricky. It took me a long while to write the one at http://ncsu.hillelsofnc.org

Another option is to create a loop that removes all the Sundays right before the calendar is actually generated.

Good Luck!

-OmniUni
dauzy
Forum Newbie
Posts: 11
Joined: Wed Jul 16, 2008 4:38 pm

Re: Help with Making a PHP calendar!

Post by dauzy »

Thanks for the reply....

I tried the changing the 7's to 6's didn't really work out so well...and totally forgot one major point in "excluding" Sundays from the calendar. So what needs to happen is that when it IS a Sunday it would create and empty table data....

so it would be something like this:

Code: Select all

 
    while($day_num <= $days_in_month)
    {
        if(isSunday() == true)
        {
            '<td class="sunday_calendar_data"><div id=day_num></div></td>';
        }
        
        else{
        //If the $day_num is included in $directory new table data with corresponded .jpg will be used. 
        if(in_array($day_num, $directory))
        {
            $event = "images/_calendar/July/calendar/".$day_num.".jpg";
            echo '<td class="calendar_data" background="'.$event.'"><div id=day_num>'.$day_num.'</div></td>';
            $day_num++;
            $day_count++;   
        }
        else
        {
            //If $day_num is not included in $directory new table data will be displayed with the default.jpg
            echo '<td class="calendar_data" background="images/_calendar/July/calendar/default.jpg"><div id=day_num>'.$day_num.'</div></td>';
            $day_num++;
            $day_count++;
    }
 
        //This will ensure table row in the calendar will only have 7 days to it. 
        if($day_count > 7)
        {
        echo "</tr><tr>";
        $day_count = 1;
    }
}
 
Now I'm having trouble on what the isSunday( ) would do... i know it should check if the day is a sunday or not.... but problem is that the $day_num variable isn't tagged on a certain "day". So when creating the calendar it just puts the number across not know what day it is.... hope thats understandable...

thanks =)
Last edited by dauzy on Wed Jul 16, 2008 5:51 pm, edited 1 time in total.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Help with Making a PHP calendar!

Post by omniuni »

why don't you just use a css class isSunday, and...

Code: Select all

.isSunday{
visibility: hidden;
}
which will simply make it invisible?

Also, perhaps you could use modulo to know if it is a Sunday, or rather, the 7th day.

Code: Select all

if($daynum%7 == 0){
//Day is Sunday
}
Last edited by omniuni on Wed Jul 16, 2008 5:34 pm, edited 1 time in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Help with Making a PHP calendar!

Post by Benjamin »

Can both of you use the code tags please.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Help with Making a PHP calendar!

Post by omniuni »

Heh, sorry. I forget that the [ code ] tag actually works on this forum.
dauzy
Forum Newbie
Posts: 11
Joined: Wed Jul 16, 2008 4:38 pm

Re: Help with Making a PHP calendar!

Post by dauzy »

Okay so heres what I got so far and still with no avail i have not been able to take out all the sundays (which are now mondays, boss changed it on me) taken out

Code: Select all

 
 
function makeCalendar()
{
    //Create a varible directory, which holds information that is taken from the scan() funtion. 
    $directory = scan();
    //Variables for the day, month and year
    $date = time ();
    $day = date('d', $date);
    $month = date('m', $date);
    $year = date('Y', $date);
    
    
    //Finds the first day of the month
    
    $first_day = mktime(0,0,0,$month, 1, $year);
 
 
    $something = date('w', $first_day);
 
    //echo $something;
 
    //Calculates the day of the week that the first day is on
    $day_of_week = date('D', $first_day);
    
    
 
    //Sets the the day to a numerical value... Sun = 0...Sat = 6
    switch($day_of_week){
    case "Mon": $blank = 0; break;
    case "Tue": $blank = 1; break;
    case "Wed": $blank = 2; break;
    case "Thu": $blank = 3; break;
    case "Fri": $blank = 4; break;
    case "Sat": $blank = 5; break;
    case "Sun": $blank = 6; break;
    } 
 
    //Calculates the days in the month
    $days_in_month = cal_days_in_month(0, $month, $year);
    $day_count = 1;
 
    //This while loop creates the blank spaces before the first day of the month.
    while($blank > 0)
    {
        echo "<td></td>";
        $blank = $blank-1;
        $day_count++;
    }
 
    $day_num = 1;
 
    
    $something_again = mktime(0,0,0,$month, $day_num, $year); 
    //echo $something_again;
    $oh_day = date('w', $something_again);
    //echo $oh_day;
    
    
    //This while loop is used to create a table row and table ddta.
    while($day_num <= $days_in_month)
    {
        $what_day = isMonday();
        //echo $what_day;
        if($what_day == 1)
        {
            echo '<td class="sunday_calendar_data"></td>';
            $day_num++;
            $day_count++;
        }
        
        else{
        //If the $day_num is included in $directory new table data with corresponded .jpg will be used. 
        if(in_array($day_num, $directory))
        {
            $event = "images/_calendar/July/calendar/".$day_num.".jpg";
            echo '<td class="calendar_data" background="'.$event.'"><div id=day_num>'.$day_num.'</div></td>';
            $day_num++;
            $day_count++;   
        }
        else
        {
            //If $day_num is not included in $directory new table data will be displayed with the default.jpg
            echo '<td class="calendar_data" background="images/_calendar/July/default.jpg"><div id=day_num>'.$day_num.'</div></td>';
            $day_num++;
            $day_count++;
        }
    }
 
        //This will ensure table row in the calendar will only have 7 days to it. 
        if($day_count > 7)
        {
        echo "</tr><tr>";
        $day_count = 1;
    }
}
    
    //This will ensure table row in the calendar will only have 7 days to it. 
    while($day_count > 1 && $day_count <= 7)
    {
        echo "<td></td>";
        $day_count++;
    }
    
    
}
 
......
 
function isMonday()
{
    $something_again = mktime(0,0,0,$month, $day_num, $year); 
    //echo $something_again;
    $oh_day = date('w', $something_again);
    return $oh_day;
 
 
}
 
 
 
closest I got so far was just the first day of the month was taken out >.< I feel like theres something with the code logically or something wrong with my calculations....


Heres what I'm trying to do... im trying to compare $day_num with the current day date('w', $something_again); <--- which should represent the current day table row that the function is making... hope thats understandable.... thanks for the input everyone...

Dauzy
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Help with Making a PHP calendar!

Post by omniuni »

Hm. I don't know if it will help, but if you want, I can send you the PHP for my calendar script. It has two "displaycal" functions, which are very easy to modify in terms of logic, so it should be easy enough to have it omit the day of the week. I think part of the problem is that your script seems to run through once, and displays the calendar kind of "along the way" as opposed to generating the data, and then having a function dedicated to merely drawing the thing (in a more object-oriented pattern).
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Help with Making a PHP calendar!

Post by omniuni »

Got it: (This version removes monday!)

Code: Select all

<table>
<?php
 
 //Create a varible directory, which holds information that is taken from the scan() funtion.
    //$directory = scan();
    //Variables for the day, month and year
    $date = time ();
    $day = date('d', $date);
    $month = date('m', $date);
    $year = date('Y', $date);
  
    //Finds the first day of the month
    $first_day = mktime(0,0,0,$month, 1, $year);
  
    //Calculates the day of the week that the first day is on
    $day_of_week = date('D', $first_day);
  
    //Sets the the day to a numerical value... Sun = 0...Sat = 6
    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;
    }
  
    //Calculates the days in the month
    $days_in_month = cal_days_in_month(0, $month, $year);
  
  
    $day_count = 1;
  
    //This while loop creates the blank spaces before the first day of the month.
    while($blank > 0)
    {
 
    if($day_count == 2){echo "<td style=\"width: 0px;\"></td>"; $blank--;}else{
       echo "<td></td>";
       $blank = $blank-1;
    }
 
       $day_count++;
    }
  
    $day_num = 1;
  
    //This while loop is used to create a table row and table ddta.
    while($day_num <= $days_in_month)
    {
       //If the $day_num is included in $directory new table data with corresponded .jpg will be used.
       if(false)
       {
          $event = "images/_calendar/July/calendar/".$day_num.".jpg";
          echo '<td width="145px" height="145px" background="'.$event.'">'.$day_num.'</td>';
          $day_num++;
          $day_count++;  
       }
       else
       {
    
    if($day_count == 2){echo "<td style=\"width: 0px;\"></td>";}else{
          //If $day_num is not included in $directory new table data will be displayed with the default.jpg
          echo '<td width="145px" height="145px" background="images/_calendar/July/calendar/default.jpg">'.$day_num.'</td>';
    }
          $day_num++;
          $day_count++;
       }
  
       //This will ensure table row in the calendar will only have 7 days to it.
       if($day_count > 7)
       {
       echo "</tr><tr>";
       $day_count = 1;
       }
    }
    
     //This will ensure table row in the calendar will only have 7 days to it.
    while($day_count > 1 && $day_count <= 6)
    {
       echo "<td></td>";
       $day_count++;
    }
 
?>
</table>
Post Reply