Results 1 to 3 of 3

To create a calendar script in PHP

This is a discussion on To create a calendar script in PHP within the Programming forums, part of the Web Designing & Development category; Step 1. First of all try to collect the necessary information which are important to display the actual month and ...

  1. #1
    Senior Member Array akash's Avatar
    Join Date
    Oct 2008
    Location
    Heaven (Earth)
    Posts
    500

    Default To create a calendar script in PHP

    Step 1.
    First of all try to collect the necessary information which are important to display the actual month and highlight the actual day. Besides this we want to display the actual month and year informations as well. To do this we need 3 special days informations:

    1. The actual day
    2. The first day of the actual month
    3. The last day of the actual month


    With the above mentioned informations we can decide what day was the first day, how long is the month and of course which is the actual day.

    Step 2.
    To get the information mentioned in Step 1 we will use the PHP built in function: getdate(). Without parameters this function returns the actual day informations in an array as follows:

    Array
    (
    [seconds] => 40
    [minutes] => 58
    [hours] => 21
    [mday] => 17
    [wday] => 2
    [mon] => 6
    [year] => 2003
    [yday] => 167
    [weekday] => Tuesday
    [month] => June
    [0] => 1055901520
    )

    To get the last day of the month with getdate we need to try to get the 0. day of the next month. So the code to get the information looks like this:
    Code:
         <?php
        $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']));
    ?>
    Step 3.To display a calendar we need a table with 7 columns for the days of the week. The number of lines depending on the number of days and the first day of the month. However we need a header line with month and year information, a subheader line with the name of the days.
    Code:
         <?php
        // 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>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
        echo '  <td>Fr</td><td>Sa</td><td>Su</td></tr>';
    ?>
    Step 4.Ok, now we have the header of the table. Let's try to fill the first row. It is not so easy as you can not just write 1 in the first cell, 2 in the second and so on. It only works if the first day of the month was Monday, but what if not? To decide this we need the wday item from the firstDay array. With this information we can fill the cells with a space if needed. The code to do this is the follows:
    Code:
         <?php
        echo '<tr>';
        for($i=1;$i<$firstDay['wday'];$i++){
            echo '<td>&nbsp;</td>';
        }
        $actday = 0;
        for($i=$firstDay['wday'];$i<=7;$i++){
            $actday++;
            echo "<td>$actday</td>";
        }
        echo '</tr>';
    ?>
    Step 5.As next step we need to fill to following lines. It is a bit easier, we only need to know how many full week we have and fill some table rows as follows:
    Code:
         <?php
        $fullWeeks = floor(($lastDay['mday']-$actday)/7);
        
        for ($i=0;$i<$fullWeeks;$i++){
            echo '<tr>';
            for ($j=0;$j<7;$j++){
                $actday++;
                echo "<td>$actday</td>";
            }
            echo '</tr>';
        }
    ?>
    Step 6.As semi final step we need to add the rest of the month to the last line. In this case it is quite easy:
    Code:
         <?php
        if ($actday < $lastDay['mday']){
            echo '<tr>';
            
            for ($i=0; $i<7;$i++){
                $actday++;
                if ($actday <= $lastDay['mday']){
                    echo "<td>$actday</td>";
                }
                else {
                    echo '<td>&nbsp;</td>';
                }
            }
            
            echo '</tr>';
        }
    ?>
    Step 7.To make the calendar little bit nicer we will introduce some CSS design. The CSS file is very simple:
    Code:
         table {
        width:210px;
        border:0px solid #888;    
        border-collapse:collapse;
    }
    
    td {
        width:30px;
        border-collpase:collpase;
        border:1px solid #888;
        text-align:right;
        padding-right:5px;
    }
    
    .days{
        background-color: #F1F3F5;
    }
    
    th {
        border-collpase:collpase;
        border:1px solid #888;
        background-color: #E9ECEF;
    }
    
    .actday{
        background-color: #c22;
        font-weight:bold;
    }
    Step 8.
    The complete code using the CSS is the following:
    Code:
         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
    <html>
    <head>
       <link href="style/style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    
    <?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>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
        echo '  <td>Fr</td><td>Sa</td><td>Su</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>$actday</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>$actday</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>$actday</td>";
                }
                else {
                    echo '<td>&nbsp;</td>';
                }
            }
            
            
            echo '</tr>';
        }
        
        echo '</table>';
    }
    
    showCalendar();
    ?>
    
    </body>
    </html>

  2. #2
    Senior Member Array akash's Avatar
    Join Date
    Oct 2008
    Location
    Heaven (Earth)
    Posts
    500

    Default Re: To create a calendar script in PHP

    here I have another script for php calender....
    Code:
    <?
    
    class Calendar
    {
    
        function Calendar()
        {
        }
        
        function getDayNames()
        {
            return $this->dayNames;
        }
        
    
        function setDayNames($names)
        {
            $this->dayNames = $names;
        }
        
        function getMonthNames()
        {
            return $this->monthNames;
        }
        
        function setMonthNames($names)
        {
            $this->monthNames = $names;
        }
        
          function getStartDay()
        {
            return $this->startDay;
        }
        function setStartDay($day)
        {
            $this->startDay = $day;
        }
        
        function getStartMonth()
        {
            return $this->startMonth;
        }
        
        function setStartMonth($month)
        {
            $this->startMonth = $month;
        }
        
        function getCalendarLink($month, $year)
        {
            return "";
        }
        
        function getDateLink($day, $month, $year)
        {
            return "";
        }
    
    
        function getCurrentMonthView()
        {
            $d = getdate(time());
            return $this->getMonthView($d["mon"], $d["year"]);
        }
        
    
        function getCurrentYearView()
        {
            $d = getdate(time());
            return $this->getYearView($d["year"]);
        }
        
        
        function getMonthView($month, $year)
        {
            return $this->getMonthHTML($month, $year);
        }
        
    
        function getYearView($year)
        {
            return $this->getYearHTML($year);
        }
          
        
    
        function getDaysInMonth($month, $year)
        {
            if ($month < 1 || $month > 12)
            {
                return 0;
            }
       
            $d = $this->daysInMonth[$month - 1];
       
            if ($month == 2)
            {
            
                if ($year%4 == 0)
                {
                    if ($year%100 == 0)
                    {
                        if ($year%400 == 0)
                        {
                            $d = 29;
                        }
                    }
                    else
                    {
                        $d = 29;
                    }
                }
            }
        
            return $d;
        }
    
    
        function getMonthHTML($m, $y, $showYear = 1)
        {
            $s = "";
            
            $a = $this->adjustDate($m, $y);
            $month = $a[0];
            $year = $a[1];        
            
            $daysInMonth = $this->getDaysInMonth($month, $year);
            $date = getdate(mktime(12, 0, 0, $month, 1, $year));
            
            $first = $date["wday"];
            $monthName = $this->monthNames[$month - 1];
            
            $prev = $this->adjustDate($month - 1, $year);
            $next = $this->adjustDate($month + 1, $year);
            
            if ($showYear == 1)
            {
                $prevMonth = $this->getCalendarLink($prev[0], $prev[1]);
                $nextMonth = $this->getCalendarLink($next[0], $next[1]);
            }
            else
            {
                $prevMonth = "";
                $nextMonth = "";
            }
            
            $header = $monthName . (($showYear > 0) ? " " . $year : "");
            
            $s .= "<table class=\"calendar\">\n";
            $s .= "<tr>\n";
            $s .= "<td align=\"center\" valign=\"top\">" . (($prevMonth == "") ? "&nbsp;" : "<a href=\"$prevMonth\">&lt;&lt;</a>")  . "</td>\n";
            $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\" colspan=\"5\">$header</td>\n"; 
            $s .= "<td align=\"center\" valign=\"top\">" . (($nextMonth == "") ? "&nbsp;" : "<a href=\"$nextMonth\">&gt;&gt;</a>")  . "</td>\n";
            $s .= "</tr>\n";
            
            $s .= "<tr>\n";
            $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay)%7] . "</td>\n";
            $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+1)%7] . "</td>\n";
            $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+2)%7] . "</td>\n";
            $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+3)%7] . "</td>\n";
            $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+4)%7] . "</td>\n";
            $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+5)%7] . "</td>\n";
            $s .= "<td align=\"center\" valign=\"top\" class=\"calendarHeader\">" . $this->dayNames[($this->startDay+6)%7] . "</td>\n";
            $s .= "</tr>\n";
            
        
            $d = $this->startDay + 1 - $first;
            while ($d > 1)
            {
                $d -= 7;
            }
    
        
            $today = getdate(time());
            
            while ($d <= $daysInMonth)
            {
                $s .= "<tr>\n";       
                
                for ($i = 0; $i < 7; $i++)
                {
                    $class = ($year == $today["year"] && $month == $today["mon"] && $d == $today["mday"]) ? "calendarToday" : "calendar";
                    $s .= "<td class=\"$class\" align=\"right\" valign=\"top\">";       
                    if ($d > 0 && $d <= $daysInMonth)
                    {
                        $link = $this->getDateLink($d, $month, $year);
                        $s .= (($link == "") ? $d : "<a href=\"$link\">$d</a>");
                    }
                    else
                    {
                        $s .= "&nbsp;";
                    }
                      $s .= "</td>\n";       
                    $d++;
                }
                $s .= "</tr>\n";    
            }
            
            $s .= "</table>\n";
            
            return $s;      
        }
        
        
        function getYearHTML($year)
        {
            $s = "";
            $prev = $this->getCalendarLink(0, $year - 1);
            $next = $this->getCalendarLink(0, $year + 1);
            
            $s .= "<table class=\"calendar\" border=\"0\">\n";
            $s .= "<tr>";
            $s .= "<td align=\"center\" valign=\"top\" align=\"left\">" . (($prev == "") ? "&nbsp;" : "<a href=\"$prev\">&lt;&lt;</a>")  . "</td>\n";
            $s .= "<td class=\"calendarHeader\" valign=\"top\" align=\"center\">" . (($this->startMonth > 1) ? $year . " - " . ($year + 1) : $year) ."</td>\n";
            $s .= "<td align=\"center\" valign=\"top\" align=\"right\">" . (($next == "") ? "&nbsp;" : "<a href=\"$next\">&gt;&gt;</a>")  . "</td>\n";
            $s .= "</tr>\n";
            $s .= "<tr>";
            $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(0 + $this->startMonth, $year, 0) ."</td>\n";
            $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(1 + $this->startMonth, $year, 0) ."</td>\n";
            $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(2 + $this->startMonth, $year, 0) ."</td>\n";
            $s .= "</tr>\n";
            $s .= "<tr>\n";
            $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(3 + $this->startMonth, $year, 0) ."</td>\n";
            $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(4 + $this->startMonth, $year, 0) ."</td>\n";
            $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(5 + $this->startMonth, $year, 0) ."</td>\n";
            $s .= "</tr>\n";
            $s .= "<tr>\n";
            $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(6 + $this->startMonth, $year, 0) ."</td>\n";
            $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(7 + $this->startMonth, $year, 0) ."</td>\n";
            $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(8 + $this->startMonth, $year, 0) ."</td>\n";
            $s .= "</tr>\n";
            $s .= "<tr>\n";
            $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(9 + $this->startMonth, $year, 0) ."</td>\n";
            $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(10 + $this->startMonth, $year, 0) ."</td>\n";
            $s .= "<td class=\"calendar\" valign=\"top\">" . $this->getMonthHTML(11 + $this->startMonth, $year, 0) ."</td>\n";
            $s .= "</tr>\n";
            $s .= "</table>\n";
            
            return $s;
        }
        function adjustDate($month, $year)
        {
            $a = array();  
            $a[0] = $month;
            $a[1] = $year;
            
            while ($a[0] > 12)
            {
                $a[0] -= 12;
                $a[1]++;
            }
            
            while ($a[0] <= 0)
            {
                $a[0] += 12;
                $a[1]--;
            }
            
            return $a;
        }
        var $startDay = 0;
    
    
        var $startMonth = 1;
    
        var $dayNames = array("S", "M", "T", "W", "T", "F", "S");
        
        var $monthNames = array("January", "February", "March", "April", "May", "June",
                                "July", "August", "September", "October", "November", "December");
                                
               
        var $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
        
    }
    
    ?>

  3. #3
    Senior Member Array
    Join Date
    Mar 2011
    Posts
    110

    Default Re: To create a calendar script in PHP

    Actually I am a new learner in PHP ,it is very useful for me,I want the code thank you very much.......



Similar Threads

  1. php script for create a watermark image.
    By orlando in forum Programming
    Replies: 5
    Last Post: 03-29-2011, 05:48 AM
  2. Best PHP / MySQL Based Calendar Solution?
    By dreamzdb in forum Scripts & Content Management
    Replies: 2
    Last Post: 05-26-2009, 07:52 AM
  3. How to create a sitemap
    By SticKer in forum Advertising & Promotion
    Replies: 12
    Last Post: 04-13-2009, 10:56 AM
  4. Replies: 3
    Last Post: 02-03-2009, 07:40 AM
  5. New calendar marketing concept - Calgoo.com
    By petrakeawel in forum Advertising & Promotion
    Replies: 1
    Last Post: 11-06-2008, 11:36 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO Forum | Web Hosting Forum | Websites For Sale |