Jump to content

Started the Calendar


ChompGator

Recommended Posts

Hey Everyone,

 

I started coding my calendar, and right now it is just showing April...What I want to do is this

`<<  April  >>`    And the arrows will control the month - Adding the arrows are simple, basic html, but how do I get it so when the arrows are clicked they go to the next or previous month and show the correct days for that month. I haven't attempted it yet...I wanted to get some input before I go further, any help is great, Im going to read some more on google too - here is the code so far:

 

<?php

    // get this month and this years as an int
    $thismonth = ( int ) date( "m" );
    $thisyear = date( "Y" );
    
    // Number of Days
    $numdaysinmonth = cal_days_in_month( CAL_GREGORIAN, $thismonth, $thisyear );
    
    // Calendar object
    $jd = cal_to_jd( CAL_GREGORIAN, date( "m" ),date( 1 ), date( "Y" ) );
    
    // get the start day as an int (0 = Sunday, 1 = Monday, etc)
    $startday = jddayofweek( $jd , 0 );
    
    // get the month as a name
    $monthname = jdmonthname( $jd, 1 )

?><center>
<table>
    <tr>
        <td colspan="7"><div align="center"><strong><?= $monthname ?></strong></div></td>
    </tr>
    <tr>
        <td><strong>S</strong></td>
        <td><strong>M</strong></td>
        <td><strong>T</strong></td>
        <td><strong>W</strong></td>
        <td><strong>T</strong></td>
        <td><strong>F</strong></td>
        <td><strong>S</strong></td>
    </tr>
    <tr></center>
<?php

    // put render empty cells

    $emptycells = 0;

    for( $counter = 0; $counter <  $startday; $counter ++ ) {
    
        echo "\t\t<td>-</td>\n";
        $emptycells ++;
    
    }
    
    // renders the days
    
    $rowcounter = $emptycells;
    $numinrow = 7;
    
    for( $counter = 1; $counter <= $numdaysinmonth; $counter ++ ) {
    
        $rowcounter ++;
        
        echo "\t\t<td>$counter</td>\n";
        
        if( $rowcounter % $numinrow == 0 ) {
        
            echo "\t</tr>\n";
            
            if( $counter < $numdaysinmonth ) {
            
                echo "\t<tr>\n";
            
            }
        
            $rowcounter = 0;
        
        }
    
    }
    
    // clean up
    $numcellsleft = $numinrow - $rowcounter;
    
    if( $numcellsleft != $numinrow ) {
    
        for( $counter = 0; $counter < $numcellsleft; $counter ++ ) {
        
            echo "\t\t<td>-</td>\n";
            $emptycells ++;
        
        }
    
    }

?>
    </tr>
</table>
</body>
 </td>
                    </tr>
                  </table>

 

Link to comment
Share on other sites

try

<?php

$month = isset($_GET['month']) ? $_GET['month']  : date('n');
$year  = isset($_GET['year'])  ? $_GET['year']   : date('Y');

$monthname = date('F', mktime (0,0,0,$month,1,$year));
$prevM = $month==1 ? 12 : $month-1;
$prevY = $month==1 ? $year - 1 : $year;
$nextM = $month==12 ? 1 : $month+1;
$nextY = $month==12 ? $year + 1 : $year;

echo "<a href='?month=$prevM&year=$prevY'><<</a> $monthname $year <a href='?month=$nextM&year=$nextY'>>></a>";
?>

Link to comment
Share on other sites

Here is what the script looks like now:

 

Its not showing the calendar but its allowing me to scroll back and fourth between months.

 

<?php
$month = isset($_GET['month']) ? $_GET['month']  : date('n');
$year  = isset($_GET['year'])  ? $_GET['year']   : date('Y');

$monthname = date('F', mktime (0,0,0,$month,1,$year));
$prevM = $month==1 ? 12 : $month-1;
$prevY = $month==1 ? $year - 1 : $year;
$nextM = $month==12 ? 1 : $month+1;
$nextY = $month==12 ? $year + 1 : $year;

echo "<a href='?month=$prevM&year=$prevY'><<</a> $monthname $year <a href='?month=$nextM&year=$nextY'>>></a>";

?><center>
<table>
    <tr>
        <td colspan="7"><div align="center"><strong><?= $monthname ?></strong></div></td>
    </tr>
    <tr>
        <td><strong>S</strong></td>
        <td><strong>M</strong></td>
        <td><strong>T</strong></td>
        <td><strong>W</strong></td>
        <td><strong>T</strong></td>
        <td><strong>F</strong></td>
        <td><strong>S</strong></td>
    </tr>
    <tr></center>
<?php

    // put render empty cells

    $emptycells = 0;

    for( $counter = 0; $counter <  $startday; $counter ++ ) {
    
        echo "\t\t<td>-</td>\n";
        $emptycells ++;
    
    }
    
    // renders the days
    
    $rowcounter = $emptycells;
    $numinrow = 7;
    
    for( $counter = 1; $counter <= $numdaysinmonth; $counter ++ ) {
    
        $rowcounter ++;
        
        echo "\t\t<td>$counter</td>\n";
        
        if( $rowcounter % $numinrow == 0 ) {
        
            echo "\t</tr>\n";
            
            if( $counter < $numdaysinmonth ) {
            
                echo "\t<tr>\n";
            
            }
        
            $rowcounter = 0;
        
        }
    
    }
    
    // clean up
    $numcellsleft = $numinrow - $rowcounter;
    
    if( $numcellsleft != $numinrow ) {
    
        for( $counter = 0; $counter < $numcellsleft; $counter ++ ) {
        
            echo "\t\t<td>-</td>\n";
            $emptycells ++;
        
        }
    
    }

?>
    </tr>
</table>
</body>
 </td>
                    </tr>
                  </table>

Link to comment
Share on other sites

Yep, no thats excatly what I wanted...Its just that its letting me scroll back and fourth between months, but its not changing the calendar dates.

 

(Ill send you a pvt with the URL you can go to, to take a look at it)

 

Current Code:

 

<?php
$month = isset($_GET['month']) ? $_GET['month']  : date('n');
$year  = isset($_GET['year'])  ? $_GET['year']   : date('Y');

$monthname = date('F', mktime (0,0,0,$month,1,$year));
$prevM = $month==1 ? 12 : $month-1;
$prevY = $month==1 ? $year - 1 : $year;
$nextM = $month==12 ? 1 : $month+1;
$nextY = $month==12 ? $year + 1 : $year;

echo "<a href='?month=$prevM&year=$prevY'><<</a> $monthname $year <a href='?month=$nextM&year=$nextY'>>></a>";

?><center>
<table>
    <tr>
        <td colspan="7"><div align="center"><strong><?= $monthname ?></strong></div></td>
    </tr>
    <tr>
        <td><strong>S</strong></td>
        <td><strong>M</strong></td>
        <td><strong>T</strong></td>
        <td><strong>W</strong></td>
        <td><strong>T</strong></td>
        <td><strong>F</strong></td>
        <td><strong>S</strong></td>
    </tr>
    <tr></center>
<?php

    // put render empty cells

    $emptycells = 0;

    for( $counter = 0; $counter <  $startday; $counter ++ ) {
    
        echo "\t\t<td>-</td>\n";
        $emptycells ++;
    
    }
    
    // renders the days
    
    $rowcounter = $emptycells;
    $numinrow = 7;
    
    for( $counter = 1; $counter <= $numdaysinmonth; $counter ++ ) {
    
        $rowcounter ++;
        
        echo "\t\t<td>$counter</td>\n";
        
        if( $rowcounter % $numinrow == 0 ) {
        
            echo "\t</tr>\n";
            
            if( $counter < $numdaysinmonth ) {
            
                echo "\t<tr>\n";
            
            }
        
            $rowcounter = 0;
        
        }
    
    }
    
    // clean up
    $numcellsleft = $numinrow - $rowcounter;
    
    if( $numcellsleft != $numinrow ) {
    
        for( $counter = 0; $counter < $numcellsleft; $counter ++ ) {
        
            echo "\t\t<td>-</td>\n";
            $emptycells ++;
        
        }
    
    }

?>
    </tr>
</table>
</body>
 </td>
                    </tr>
                  </table>

Link to comment
Share on other sites

Here's one I wrote about 3 years ago

<html>
<!-- Creation date: 04/02/05 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>baaCalendar</title>
<meta name="Author" content="Barand">
<meta name="Generator" content="AceHTML 4 Pro">

<style type="text/css">
TD {font-family: arial; font-size: 9pt; text-align: center; height: 20}
TH {font-family: arial; font-size: 9pt; text-align: center; width: 20px; height: 20; font-weight: 600; color: #FFFFFF}
TH.wd {background: #99CC99}
TH.we {background: #999999}
TD.we {background: #EEEEEE; }
TD.wd {background: #EEFFEE}
TD.hd {background: #FFFFFF; color: #339900}
A.nul {text-decoration: none}
A:hover {background: #DDDDDD; color: #FF0000}
</style>
</head>
<body>

<?php
    $currMonth = isset($_GET['month']) ? $_GET['month']  : date('n');
    $currYear  = isset($_GET['year'])  ? $_GET['year']   : date('Y');
    $today = (($currYear == date('Y')) && ($currMonth == date('n'))) ? date('j') : 0;

    $prevMonth = $currMonth==1 ? 12 : $currMonth-1;
    $nextMonth = $currMonth==12?  1 : $currMonth+1;
    $prevYear = $currMonth==1 ? $currYear-1 : $currYear;
    $nextYear = $currMonth==12? $currYear+1 : $currYear;


    $day1 = mktime(0,0,0,$currMonth,1,$currYear);
    $dim = date('t', $day1);
    $dayN = mktime(0,0,0,$currMonth,$dim,$currYear);
    $dow1 = (date('w',$day1)+6) % 7;
    $dowN = (date('w',$dayN)+6) % 7;
    $calHead = date('F Y',$day1);
    echo <<<EOT
        <table border="0" cellspacing="1" style="border: 1pt solid silver">
        <tr>
          <td class="hd"><a class="nul" href="$_SERVER[php_SELF]?year=$prevYear&month=$prevMonth"><<</a></td>
          <td colspan="5" class="hd">$calHead</td>
          <td class="hd"><a class="nul" href="$_SERVER[php_SELF]?year=$nextYear&month=$nextMonth">>></a></td>
        </tr>
        <tr>
          <th class="wd">M</th>
          <th class="wd">T</th>
          <th class="wd">W</th>
          <th class="wd">T</th>
          <th class="wd">F</th>
          <th class="we">S</th>
          <th class="we">S</th>
        </tr>
        <tr>
EOT;

    for ($d=0;$d<$dow1;$d++) echo "<td class=\"hd\"> </td>";
    $c = $dow1;
    for ($d=1; $d<=$dim; $d++, $c++) {
        if ($c%7==0) echo "</tr><tr>";
        $cl = ($c%7==5) || ($c%7==6) ? 'we' : 'wd';
        $st = ($d == $today) ? "style='border: 1pt solid #FF0000'" : '';
        echo "<td class=\"$cl\" $st>\n";
        echo "$d" ;
        echo "</td>\n";
    }
    while ($c++ % 7 != 0) echo '<td class=\"hd\"> </td>';
    echo "</tr></table>\n";


?>
</body>
</html>

Link to comment
Share on other sites

Hey man,

 

That is fantastic! Thanks a bunch for that! Funny thing, I just got mine too work before you posted yours, but yours looks more stylish hah, so Im going to use it!

 

...Now what Im going to try and do is make it > so when you hover over a number on the calendar > a little window to left-side of the mouse arrow will appear > and display what event is on that day! Should be more of a challenge!

 

First I have to make an area where users can post an event and that information goes into the database > then in the window that appears Im just going to get it to call whatever event is on that day from the database and display it in the little window that appears...when you hover your mouse over a certain date. Im sure Ill have trouble with that too, but Im going to start coding!

 

Anyway thanks again!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.