Calendar in php

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
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Calendar in php

Post by xionhack »

Hello. I have a code that shows a calendar for the month. I want that inside each box with the day, to display the whole date. Meaning, in the box where it has the 1 for January, for it to show "01/01/2009". If you have a better code please let me know. I need put a personal scheduler in a data base that I'm making, but non of the free codes that I've found have helped me.

Code: Select all

<?php
function showCalendar(){
    // Get key day informations. 
    // We need the first and last day of the month and the actual day
    $today    = getdate();
    $firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
    $lastDay  = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));
    
    
    // Create a table with the necessary header informations
    echo '<table>';
    echo '  <tr><th colspan="7">'.$today['month']." - ".$today['year']."</th></tr>";
    echo '<tr class="days">';
    echo '  <td>Monday</td><td>Tuesday</td><td>Wednesday</td><td>Thursday</td>';
    echo '  <td>Friday</td><td>Saturday</td><td>Sunday</td></tr>';
    
    
    // Display the first calendar row with correct positioning
    echo '<tr>';
    for($i=1;$i<$firstDay['wday'];$i++){
        echo '<td>&nbsp;</td>';
    }
    $actday = 0;
    for($i=$firstDay['wday'];$i<=7;$i++){
        $actday++;
        if ($actday == $today['mday']) {
            $class = ' class="actday"';
        } else {
            $class = '';
        }
        echo "<td$class><div id=\"day_box\"><a href=\"#\">$actday</a></div>{$today['wday']}</td>";
    }
    echo '</tr>';
    
    //Get how many complete weeks are in the actual month
    $fullWeeks = floor(($lastDay['mday']-$actday)/7);
    
    for ($i=0;$i<$fullWeeks;$i++){
        echo '<tr>';
        for ($j=0;$j<7;$j++){
            $actday++;
            if ($actday == $today['mday']) {
                $class = ' class="actday"';
            } else {
                $class = '';
            }
            echo "<td$class><div id=\"day_box\"><a href=\"#\">$actday</a></div></td>";
        }
        echo '</tr>';
    }
    
    //Now display the rest of the month
    if ($actday < $lastDay['mday']){
        echo '<tr>';
        
        for ($i=0; $i<7;$i++){
            $actday++;
            if ($actday == $today['mday']) {
                $class = ' class="actday"';
            } else {
                $class = '';
            }
            
            if ($actday <= $lastDay['mday']){
                echo "<td$class><div id=\"day_box\"><a href=\"#\">$actday</a></div></td>";
            }
            else {
                echo '<td>&nbsp;</td>';
            }
        }
        
        
        echo '</tr>';
    }
    
    echo '</table>';
}
 
showCalendar();
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: Calendar in php

Post by it2051229 »

how about this? I added some few codes.. give it a try

Code: Select all

 
<?php
function showCalendar(){
    // Get key day informations.
    // We need the first and last day of the month and the actual day
    $today    = getdate();
    $firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
    $lastDay  = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));
    
    // get the current year
    $year = date("Y"); // year in full text 2008
    
    // get the current month
    $month = date("n");
   
   
    // Create a table with the necessary header informations
    echo '<table>';
    echo '  <tr><th colspan="7">'.$today['month']." - ".$today['year']."</th></tr>";
    echo '<tr class="days">';
    echo '  <td>Monday</td><td>Tuesday</td><td>Wednesday</td><td>Thursday</td>';
    echo '  <td>Friday</td><td>Saturday</td><td>Sunday</td></tr>';
   
   
    // Display the first calendar row with correct positioning
    echo '<tr>';
    for($i=1;$i<$firstDay['wday'];$i++){
        echo '<td>&nbsp;</td>';
    }
    $actday = 0;
    for($i=$firstDay['wday'];$i<=7;$i++){
        $actday++;
        if ($actday == $today['mday']) {
            $class = ' class="actday"';
        } else {
            $class = '';
        }
        echo "<td$class><div id=\"day_box\"><a href=\"#\">$actday</a></div>{$today['wday']}</td>";
    }
    echo '</tr>';
   
    //Get how many complete weeks are in the actual month
    $fullWeeks = floor(($lastDay['mday']-$actday)/7);
   
    for ($i=0;$i<$fullWeeks;$i++){
        echo '<tr>';
        for ($j=0;$j<7;$j++){
            $actday++;
            if ($actday == $today['mday']) {
                $class = ' class="actday"';
            } else {
                $class = '';
            }
            echo "<td$class><div id=\"day_box\"><a href=\"#\">$actday/$month/$year</a></div></td>";
        }
        echo '</tr>';
    }
   
    //Now display the rest of the month
    if ($actday < $lastDay['mday']){
        echo '<tr>';
       
        for ($i=0; $i<7;$i++){
            $actday++;
            if ($actday == $today['mday']) {
                $class = ' class="actday"';
            } else {
                $class = '';
            }
           
            if ($actday <= $lastDay['mday']){
                echo "<td$class><div id=\"day_box\"><a href=\"#\">$actday</a></div></td>";
            }
            else {
                echo '<td>&nbsp;</td>';
            }
        }
       
       
        echo '</tr>';
    }
   
    echo '</table>';
}
 
showCalendar();
?>
 
Post Reply