The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 8 of 8

Thread: help making class more Object Oriented like

  1. #1

    help making class more Object Oriented like

    I'm trying to make this class more Object-Oriented. I need to do this so it'll be easier to add events to the calendar. Check out the createMonth function.

    If you just copy and paste the following code you'll be able to see the class in action.

    Thanks

    PHP Code:
    <?php
    /**
     * A class for creating calendars
     * 
     * 
     * @author Leonel Santos
     * @copyright Leonel Santos 2009
     * @version 1
     
     


    ------- to-do ----------
    change styles and id's names, something more specific like
    .LeoCalendar-daysOfWeek
    .LeoCalendar-notThisMonth
    #LeoCalendar-today
    or
    #LeoCalendar .daysOfWeek
    #LeoCalendar .notThisMonth
    #LeoCalendar #today

    $_GET
    CalendarType
    workDate

    Styles
        on the month
            .notThisMonth
            .today
        on the week
            col#time
            col#Sunday
            col#Monday
            col#Tuesday
            col#Wednesday
            col#Thursday
            col#Friday
            col#Saturday
            th#today_col
        on the day
            tr#now


    validation: accept only integers and two dashes xxxx-xx-xx 
    error handling



            fix, next month or previous month, it just goes to the next month
            on same day    careful with Oct 31 because next month jumps to Dec
            because November only has 30 days


     */
    class LeoCalendar
    {
        protected 
    $_workDate;
        protected 
    $_workDateYMD;
        protected 
    $_calendarType;
        protected 
    $_events;
        protected 
    $_year;
        protected 
    $_month;
        protected 
    $_day;

        
    // boolean
        // 0 = is not in this month
        // 1 = it is in this month
        
    protected $_inThisMonth;

        public function 
    __construct($events=array())
        {
            if(isset(
    $_GET))
            {
            }

            if(!
    is_null($events) && !is_array($events))
            {
                throw new 
    Exception('Must be array');
            }
            
    $this->_events $events;

            if(!isset(
    $_GET['workDate']))
            {
                
    $this->_workDate date("Y-m-d");
                
    $this->_workDateYMD date('Y-m-d');
            }
            else
            {
                
    $this->_workDate $_GET['workDate'];
                
    $this->_workDateYMD date('Y-m-d'strtotime($_GET['workDate']));
            }

            
    // assign the values to the class properties
            
    $this->_year = ( int ) date'Y'strtotime($this->_workDate));
            
    $this->_month = ( int ) date'n'strtotime($this->_workDate));
            
    $this->_day = ( int ) date'j'strtotime($this->_workDate));

            if(!isset(
    $_GET['CalendarType']))
            {
                
    $this->_calendarType 'month';
            }
            else
            {
                
    $isItAllowed 0;
                
    $allowedCalendarTypes = array('month''week''day');
                foreach(
    $allowedCalendarTypes as $key => $value)
                {
                    if(
    $value==$_GET['CalendarType'])
                    {
                        
    $isItAllowed 1;
                    }
                }
                if(
    $isItAllowed==1)
                {
                    
    $this->_calendarType $_GET['CalendarType'];
                }
                else
                {
                    exit(
    'Error! Calendar Type not allowed');
                }
            }
        }

        
    ################################################
        # PUBLIC METHODS FOR blah blah blah bl         #
        ################################################
        
    public function createWeek($enterDate)
        {
            
    $today date('Y-m-d');
            
    $today_timestamp strtotime($today);

            
    $timestamp strtotime($enterDate);
            
    $timestamp_legible date('Y-m-d'$timestamp);

            if(
    date('l'$timestamp)=='Sunday')
            {
                
    $sunday_timestamp $timestamp;
                
    $sunday_legible date('F j, Y'$sunday_timestamp);
            }
            else
            {
                
    $sunday_timestamp strtotime(date('r',strtotime('last Sunday '.$timestamp_legible)));
                
    $sunday_legible date('F j, Y'$sunday_timestamp);
            }

            
    $year date('Y'$sunday_timestamp);

            
    $output '<p>
                            <a href="'
    .$_SERVER['PHP_SELF'].'?CalendarType=week&workDate='.date('Y-m-d'strtotime('-1 day '.$sunday_legible)).'">Previous Week</a>
                             | 
                            <a href="'
    .$_SERVER['PHP_SELF'].'?CalendarType=week&workDate='.date('Y-m-d'strtotime('+7 days '.$sunday_legible)).'">Next Week</a>
                        </p>
                        <table>
                            <caption>'
    .date('F j'$sunday_timestamp).' - '.date('F j, Y'strtotime('+6 days '.$sunday_legible)).'</caption>
                            <colgroup>
                                    <col id="time" />
                                    <col id="Sunday" />
                                    <col id="Monday" />
                                    <col id="Tuesday" />
                                    <col id="Wednesday" />
                                    <col id="Thursday" />
                                    <col id="Friday" />
                                    <col id="Saturday" />
                                </colgroup>
                                <thead><tr><th scope="col">'
    .$year.'</th>
                        '
    ;

            for(
    $i=0;$i<7;$i++)
            {
                
    $date date('l, M j'strtotime('+'.$i.' day '.$sunday_legible));
                
    $today_col '';
                if(
    strtotime($date) === $today_timestamp)
                {
                    
    $today_col 'id="today_col"';
                }
                else
                {
                    
    $today_col '';
                }

                
    $output .= '<th scope="col" '.$today_col.'>'.$date.'</th>';
            }
            
    $output .= '</tr></thead><tbody>';
        
            for(
    $i=6$i <= 22$i++)
            {
                
    $time strtotime('+'.$i.' hour 12AM');
                
    $hour date('H'$time);
                
    $time = ($hour != '12') ? date('g A'$time) : 'Noon';

                
    $output .= '<tr><th scope="row">'.$time.'</th>';
                for(
    $j=1$j<=7;$j++)
                {
                    
    $output .= '<td></td>';
                }
                
    $output .= '</tr>';
            }
            
    $output .= '</tbody></table>';
            return 
    $output;
        }

        public function 
    createDay($enterDate)
        {
            
    $timestamp strtotime($enterDate);
            
    $timestamp_legible date('Y-m-d'$timestamp);

            
    $output '<p>
                            <a href="'
    .$_SERVER['PHP_SELF'].'?CalendarType=day&workDate='.date('Y-m-d'strtotime('-1 day '.$timestamp_legible)).'">Previous Day</a>
                             | 
                            <a href="'
    .$_SERVER['PHP_SELF'].'?CalendarType=day&workDate='.date('Y-m-d'strtotime('+1 days '.$timestamp_legible)).'">Next Day</a>
                        </p>'
    ;

            
    $output .=    '<table>
                            <caption>'
    .date('F j'$timestamp).'</caption>
                            <thead><tr><th>'
    .date('Y'$timestamp).'</th><th>'.date('F j'$timestamp).'</th></tr></thead>
                            </tbody>
                        '
    ;

            for(
    $i=6$i <= 22$i++)
            {



                
    $time strtotime('+'.$i.' hour 12AM');
                
    $hour date('H'$time);
                
    $time = ($hour != '12') ? date('g A'$time) : 'Noon';

                
    $now '';

                if(
    $i==date('G') && $timestamp_legible==date('Y-m-d'))
                {
                    
    $now 'id="now"';
                }

                
    $output .= '<tr '.$now.'><th scope="row">'.$time.'</th><td></td>';
                
    $output .= '</tr>';
            }
            
    $output .= '</tbody></table>';
            return 
    $output;
        }

        protected function 
    GetStartingPoint($DLM)
        {
            
    $today getdate(strtotime($this->_workDate));
            
    $mday $today['mday'];
            
    $mday-=1;
            
    $FirstOfMonth date("Y-m-d",strtotime($this->_workDate "-" $mday " days"));

            switch(
    date("l",strtotime($FirstOfMonth)))
            {
            case 
    "Sunday":
                
    $CD 1;
                
    $this->_inThisMonth=1;
                break;

            case 
    "Monday":
                
    $CD $DLM;
                break;

            case 
    "Tuesday":
                
    $CD $DLM-1;
                break;

            case 
    "Wednesday":
                
    $CD $DLM-2;
                break;

            case 
    "Thursday":
                
    $CD $DLM-3;
                break;

            case 
    "Friday":
                
    $CD $DLM-4;
                break;

            case 
    "Saturday":
                
    $CD $DLM-5;
            }
            return 
    $CD;
        }

        
    /**
         * Add specified number of months to date.
         * 
         * This method adjusts the result to the final day of the month if
         * the resulting date is invalid, e.g., September 31 is converted
         * to September 30. Results in February also take account of leap
         * year. This contrasts with DateTime::modify() and strtotime, which
         * produce unexpected results by adding the day(s). 
         *
         * @param int $numMonths Number of months to be added.
         */
        
    public function addMonths($numMonths) {
            if (! 
    is_numeric $numMonths ) || $numMonths 1) {
                throw new 
    Exception 'addMonths() expects a positive integer.' );
            }
            
    $numMonths = ( int ) $numMonths;
            
    // Add the months to the current month number.
            
    $newValue $this->_month $numMonths;
            
    // If the new value is less than or equal to 12, the year
            // doesn't change, so just assign the new value to the month.
            
    if ($newValue <= 12) {
                
    $this->_month $newValue;
            } else {
                
    // A new value greater than 12 means calculating both
                // the month and the year. Calculating the year is
                // different for December, so do modulo division 
                // by 12 on the new value. If the remainder is not 0,
                // the new month is not December. 
                
    $notDecember $newValue 12;
                if (
    $notDecember) {
                    
    // The remainder of the modulo division is the new month.
                    
    $this->_month $notDecember;
                    
    // Divide the new value by 12 and round down to get the
                    // number of years to add.
                    
    $this->_year += floor $newValue 12 );
                } else {
                    
    // The new month must be December
                    
    $this->_month 12;
                    
    $this->_year += ($newValue 12) - 1;
                }
            }
            
    $this->checkLastDayOfMonth ();
            
    parent::setDate $this->_year$this->_month$this->_day );
        }
        
        
    /**
         * Subtract specified number of months from date.
         * 
         * This method adjusts the result to the final day of the month if
         * the resulting date is invalid, e.g., September 31 is converted
         * to September 30. Results in February also take account of leap
         * year. This contrasts with DateTime::modify() and strtotime, which
         * produce unexpected results by subtracting the day(s). 
         *
         * @param int $numMonths Number of months to be subtracted.
         */
        
    public function subMonths($numMonths) {
            if (! 
    is_numeric $numMonths )) {
                throw new 
    Exception 'addMonths() expects an integer.' );
            }
            
    $numMonths abs intval $numMonths ) );
            
    // Subtract the months from the current month number.
            
    $newValue $this->_month $numMonths;
            
    // If the result is greater than 0, it's still the same year,
            // and you can assign the new value to the month.
            
    if ($newValue 0) {
                
    $this->_month $newValue;
            } else {
                
    // Create an array of the months in reverse.
                
    $months range (121);
                
    // Get the absolute value of $newValue.
                
    $newValue abs $newValue );
                
    // Get the array position of the resulting month.
                
    $monthPosition $newValue 12;
                
    $this->_month $months [$monthPosition];
                
    // Arrays begin at 0, so if $monthPosition is 0,
                // it must be December.
                
    if ($monthPosition) {
                    
    $this->_year -= ceil $newValue 12 );
                } else {
                    
    $this->_year -= ceil $newValue 12 ) + 1;
                }
            }
            
    $this->checkLastDayOfMonth ();
            
    parent::setDate $this->_year$this->_month$this->_day );
        }

        
    /**
         * Create a month calendar.
         * 
         * This method returns a month grid calendar according to the 
         * workDate parameter
         *
         * @param array $events Array with events to be added to calendar.
         */
        
    protected function createMonth($events=array())
        {
    //        $previousMonth = date("Y-m-d", strtotime($this->_workDate . "-1 month"));
    //        $nextMonth = date("Y-m-d", strtotime($this->_workDate . "+1 month"));

    //        $this->_year = ( int ) date( 'Y', $this->_workDate);
    //        $this->_month = ( int ) date( 'n', $this->_workDate);
    //        $this->_day = ( int ) date( 'j', $this->_workDate);        

    //        echo '<h1>'.$this->_year.'-'.$this->_month.'-01</h1>';
    //        echo 'New next month: '.date('Y-m-d', strtotime($this->_year.'-'.$this->_month.'-01 +1 month'));

            
    $nextMonth date('Y-m-d'strtotime($this->_year.'-'.$this->_month.'-01 +1 month'));
            
    $previousMonth date('Y-m-d'strtotime($this->_year.'-'.$this->_month.'-01 -1 month'));

            
    $daysThisMonth date("t",strtotime(date("Y-m-d",strtotime($this->_workDate))));
            
    $daysLastMonth date("t",strtotime(date("Y-m-d"strtotime($previousMonth))));
            
    $this->_inThisMonth 0;
            
    $currentDate $this->GetStartingPoint($daysLastMonth);

    //        echo '<h3>previous month: '.$previousMonth.'</h3>';
    //        echo '<h3>next month: '.$nextMonth.'</h3>';
            
    echo '<h3>days this month: '.$daysThisMonth.'</h3>';
            echo 
    '<h3>days last month: '.$daysLastMonth.'</h3>';
            echo 
    '<h3>current date: '.$currentDate.'</h3>';

            if(isset(
    $events)&&!is_array($events))
            {
                exit(
    '<p>Is not array inside createMonth function!</p>');
            }

    //        $output = '<p><a href="'.$_SERVER['PHP_SELF'].'?workDate='.$previousMonth.'">Previous Month</a> | <a href="'.$_SERVER['PHP_SELF'].'?workDate='.$nextMonth.'">Next Month</a></p>';
            
    $output '<p><a href="'.$_SERVER['PHP_SELF'].'?workDate='.$previousMonth.'">Previous Month</a> | <a href="'.$_SERVER['PHP_SELF'].'?workDate='.$nextMonth.'">Next Month</a></p>';

            
    $output .= '<table><caption>'.date('F Y',strtotime($this->_workDate)).'</caption>
                            <tr>
                            <th>Sunday</th>
                            <th>Monday</th>
                            <th>Tuesday</th>
                            <th>Wednesday</th>
                            <th>Thursday</th>
                            <th>Friday</th>
                            <th>Saturday</th>
                            </tr>'
    ;


            
    // what if you find a start date, the beginning of the calendar,
            // then you set the end date. you would just have to +1 day until
            // you hit the end date. that way, you always have the day you are
            // printing, instead of just looping a for loop


            
    for($x=1;$x<7;$x++)
            {
                
    $tdDay '<tr>';
                
    $tdContent '<tr>';
                for(
    $i=0;$i<7;$i++)
                {
                    
    // place something inside the tdContent td
                    // $tdContent .= date('m',strtotime($this->_workDate));
                    // $tdInside = date('Y',strtotime($this->_workDate)).'-'.date('m',strtotime($this->_workDate)).'-'.$currentDate;
                    // $tdInside = $currentDate;

                    
    $tdContent .= '<td ';
                    
    // not from this month
                    
    if($this->_inThisMonth==0)
                    {
                        
    $tdDay .= '<td class="notThisMonth">'.$currentDate++.'</td>';
                        
    $tdContent .= 'class="notThisMonth">';

                        
    // add stuff inside day
                        // $tdContent .= ($currentDate-1).' | '.date('Y',strtotime($this->_workDate)).'-'.date("n",strtotime($this->_workDate)).'-'.($currentDate-1);
                    
    }
                    else
                    {
                        
    // today
                        
    if( $currentDate == date("j") && date("n",strtotime($this->_workDate)) == date("n") && date("y",strtotime($this->_workDate)) == date("y") )
                        {
                            
    $tdDay .= '<td class="today">'.$currentDate++.'</td>';
                            
    $tdContent .= 'class="today">';
                            
                            
    // add stuff inside day
                            // $tdContent .= date('Y',strtotime($this->_workDate)).'-'.date("n",strtotime($this->_workDate)).'-'.($currentDate-1);
                        
    }
                        
    // this month
                        
    else
                        {
                            
    $tdDay .= '<td>'.$currentDate++.'</td>';
                            
    $tdContent .= '>';

                            
    // add stuff inside day
                            // $tdContent .= date('Y',strtotime($this->_workDate)).'-'.date("n",strtotime($this->_workDate)).'-'.($currentDate-1);
                        
    }
                    }

                    
    // close tdContent td
                    
    $tdContent .= '</td>';                

                    
    // this month
                    
    if( $this->_inThisMonth == && $currentDate $daysLastMonth )
                    {
                        
    $currentDate 1;
                        
    $this->_inThisMonth 1;
                    }
                    
    // not this month
                    
    elseif($this->_inThisMonth == && $currentDate $daysThisMonth )
                    {
                        
    $currentDate 1;
                        
    $this->_inThisMonth 0;
                    }
        
                }
                
    $tdDay .= '</tr>';
                
    $tdContent .= '</tr>';
                
    $output .= $tdDay.$tdContent;
            }
            
    $output .= '</table>';
            return 
    $output;
        }

        public function 
    displayCalendar()
        {
            
    $calendarHeader '<p><a href="'.$_SERVER['PHP_SELF'].'?CalendarType=day&workDate='.$this->_workDate.'">day</a> | <a href="'.$_SERVER['PHP_SELF'].'?CalendarType=week&workDate='.$this->_workDate.'">week</a> | <a href="'.$_SERVER['PHP_SELF'].'?CalendarType=month&workDate='.$this->_workDate.'">month</a></p><p><a href="'.$_SERVER['PHP_SELF'].'?CalendarType='.$this->_calendarType.'&workDate='.date("Y-m-d").'">Today</a></p>';
            
    // change to a swich/case instead of ifelse
            
    if($this->_calendarType=='month')
            {
                return 
    $calendarHeader.$this->createMonth($this->_events);
            }
            elseif(
    $this->_calendarType=='week')
            {
                return 
    $calendarHeader.$this->createWeek($this->_workDateYMD);
            }
            elseif(
    $this->_calendarType=='day')
            {
                return 
    $calendarHeader.$this->createDay($this->_workDateYMD);
            }
        }

        
    /**
         * Determines whether the year is a leap year.
         *
         * @return bool True if it is a leap year, otherwise false.
         */
        
    public function isLeap() {
            if (
    $this->_year 400 == || ($this->_year == && $this->_year 100 != 0)) {
                return 
    true;
            } else {
                return 
    false;
            }
        }

        
    ###########################################################
        # PROTECTED METHOD                                        #
        ###########################################################
        /**
         * Adjusts the day to the last day of the month when necessary.
         * 
         * This internal method is called by addMonths(), subMonths(),
         * addYears(), and subYears(). It checks whether the date calculation
         * results in an invalid date, such as February 31. If the date is 
         * invalid, it readjusts the $_day property to the last day of
         * the month.
         */
        
    final protected function checkLastDayOfMonth() {
            if (! 
    checkdate $this->_month$this->_day$this->_year )) {
                
    $use30 = array (46911 );
                if (
    in_array $this->_month$use30 )) {
                    
    $this->_day 30;
                } else {
                    
    $this->_day $this->isLeap () ? 29 28;
                }
            }
        }
    }
    ?>
    <style>
    body { font-family:Arial, Helvetica, sans-serif; }
    table { width:100%; }
    th { background-color:#999999; }
    td { border:1px solid #999999; }
    td.notThisMonth { background-color:#CCCCCC; }
    .today { background-color:#FFFF99; }
    </style>

    <?php

    $events 
    = array('2009-11-25''2009-11-10');

    $calendar = new LeoCalendar();
    echo 
    $calendar->displayCalendar($events);
    ?>
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  2. #2
    Now the class knows, what day is every box for the days. It used to just print the numbers without knowing what day it represented.

    PHP Code:
    <?php
    /**
     * A class for creating calendars
     * 
     * 
     * @author Leonel Santos
     * @copyright Leonel Santos 2009
     * @version 1
     
     


    ------- to-do ----------
    change styles and id's names, something more specific like
    .LeoCalendar-daysOfWeek
    .LeoCalendar-notThisMonth
    #LeoCalendar-today
    or
    #LeoCalendar .daysOfWeek
    #LeoCalendar .notThisMonth
    #LeoCalendar #today

    $_GET
    CalendarType
    workDate

    Styles
        on the month
            .notThisMonth
            .today
        on the week
            col#time
            col#Sunday
            col#Monday
            col#Tuesday
            col#Wednesday
            col#Thursday
            col#Friday
            col#Saturday
            th#today_col
        on the day
            tr#now


    validation: accept only integers and two dashes xxxx-xx-xx 
    error handling



            fix, next month or previous month, it just goes to the next month
            on same day    careful with Oct 31 because next month jumps to Dec
            because November only has 30 days


     */
    class LeoCalendar
    {
        protected 
    $_workDate;
        protected 
    $_workDateYMD;
        protected 
    $_calendarType;
        protected 
    $_events;
        protected 
    $_year;
        protected 
    $_month;
        protected 
    $_day;

        
    // boolean
        // 0 = is not in this month
        // 1 = it is in this month
        
    protected $_inThisMonth;

        public function 
    __construct($events=array())
        {
            if(isset(
    $_GET))
            {
            }

            if(!
    is_null($events) && !is_array($events))
            {
                throw new 
    Exception('Must be array');
            }
            
    $this->_events $events;

            if(!isset(
    $_GET['workDate']))
            {
                
    $this->_workDate date("Y-m-d");
                
    $this->_workDateYMD date('Y-m-d');
            }
            else
            {
                
    $this->_workDate $_GET['workDate'];
                
    $this->_workDateYMD date('Y-m-d'strtotime($_GET['workDate']));
            }

            
    // assign the values to the class properties
            
    $this->_year = ( int ) date'Y'strtotime($this->_workDate));
            
    $this->_month = ( int ) date'n'strtotime($this->_workDate));
            
    $this->_day = ( int ) date'j'strtotime($this->_workDate));

            if(!isset(
    $_GET['CalendarType']))
            {
                
    $this->_calendarType 'month';
            }
            else
            {
                
    $isItAllowed 0;
                
    $allowedCalendarTypes = array('month''week''day');
                foreach(
    $allowedCalendarTypes as $key => $value)
                {
                    if(
    $value==$_GET['CalendarType'])
                    {
                        
    $isItAllowed 1;
                    }
                }
                if(
    $isItAllowed==1)
                {
                    
    $this->_calendarType $_GET['CalendarType'];
                }
                else
                {
                    exit(
    'Error! Calendar Type not allowed');
                }
            }
        }

        
    ################################################
        # PUBLIC METHODS FOR blah blah blah bl         #
        ################################################
        
    public function createWeek($enterDate)
        {
            
    $today date('Y-m-d');
            
    $today_timestamp strtotime($today);

            
    $timestamp strtotime($enterDate);
            
    $timestamp_legible date('Y-m-d'$timestamp);

            if(
    date('l'$timestamp)=='Sunday')
            {
                
    $sunday_timestamp $timestamp;
                
    $sunday_legible date('F j, Y'$sunday_timestamp);
            }
            else
            {
                
    $sunday_timestamp strtotime(date('r',strtotime('last Sunday '.$timestamp_legible)));
                
    $sunday_legible date('F j, Y'$sunday_timestamp);
            }

            
    $year date('Y'$sunday_timestamp);

            
    $output '<p>
                            <a href="'
    .$_SERVER['PHP_SELF'].'?CalendarType=week&workDate='.date('Y-m-d'strtotime('-1 day '.$sunday_legible)).'">Previous Week</a>
                             | 
                            <a href="'
    .$_SERVER['PHP_SELF'].'?CalendarType=week&workDate='.date('Y-m-d'strtotime('+7 days '.$sunday_legible)).'">Next Week</a>
                        </p>
                        <table>
                            <caption>'
    .date('F j'$sunday_timestamp).' - '.date('F j, Y'strtotime('+6 days '.$sunday_legible)).'</caption>
                            <colgroup>
                                    <col id="time" />
                                    <col id="Sunday" />
                                    <col id="Monday" />
                                    <col id="Tuesday" />
                                    <col id="Wednesday" />
                                    <col id="Thursday" />
                                    <col id="Friday" />
                                    <col id="Saturday" />
                                </colgroup>
                                <thead><tr><th scope="col">'
    .$year.'</th>
                        '
    ;

            for(
    $i=0;$i<7;$i++)
            {
                
    $date date('l, M j'strtotime('+'.$i.' day '.$sunday_legible));
                
    $today_col '';
                if(
    strtotime($date) === $today_timestamp)
                {
                    
    $today_col 'id="today_col"';
                }
                else
                {
                    
    $today_col '';
                }

                
    $output .= '<th scope="col" '.$today_col.'>'.$date.'</th>';
            }
            
    $output .= '</tr></thead><tbody>';
        
            for(
    $i=6$i <= 22$i++)
            {
                
    $time strtotime('+'.$i.' hour 12AM');
                
    $hour date('H'$time);
                
    $time = ($hour != '12') ? date('g A'$time) : 'Noon';

                
    $output .= '<tr><th scope="row">'.$time.'</th>';
                for(
    $j=1$j<=7;$j++)
                {
                    
    $output .= '<td></td>';
                }
                
    $output .= '</tr>';
            }
            
    $output .= '</tbody></table>';
            return 
    $output;
        }

        public function 
    createDay($enterDate)
        {
            
    $timestamp strtotime($enterDate);
            
    $timestamp_legible date('Y-m-d'$timestamp);

            
    $output '<p>
                            <a href="'
    .$_SERVER['PHP_SELF'].'?CalendarType=day&workDate='.date('Y-m-d'strtotime('-1 day '.$timestamp_legible)).'">Previous Day</a>
                             | 
                            <a href="'
    .$_SERVER['PHP_SELF'].'?CalendarType=day&workDate='.date('Y-m-d'strtotime('+1 days '.$timestamp_legible)).'">Next Day</a>
                        </p>'
    ;

            
    $output .=    '<table>
                            <caption>'
    .date('F j'$timestamp).'</caption>
                            <thead><tr><th>'
    .date('Y'$timestamp).'</th><th>'.date('F j'$timestamp).'</th></tr></thead>
                            </tbody>
                        '
    ;

            for(
    $i=6$i <= 22$i++)
            {



                
    $time strtotime('+'.$i.' hour 12AM');
                
    $hour date('H'$time);
                
    $time = ($hour != '12') ? date('g A'$time) : 'Noon';

                
    $now '';

                if(
    $i==date('G') && $timestamp_legible==date('Y-m-d'))
                {
                    
    $now 'id="now"';
                }

                
    $output .= '<tr '.$now.'><th scope="row">'.$time.'</th><td></td>';
                
    $output .= '</tr>';
            }
            
    $output .= '</tbody></table>';
            return 
    $output;
        }

        protected function 
    GetStartingPoint($DLM)
        {
            
    $today getdate(strtotime($this->_workDate));
            
    $mday $today['mday'];
            
    $mday-=1;
            
    $FirstOfMonth date("Y-m-d",strtotime($this->_workDate "-" $mday " days"));

            switch(
    date("l",strtotime($FirstOfMonth)))
            {
            case 
    "Sunday":
                
    $CD 1;
                
    $this->_inThisMonth=1;
                break;

            case 
    "Monday":
                
    $CD $DLM;
                break;

            case 
    "Tuesday":
                
    $CD $DLM-1;
                break;

            case 
    "Wednesday":
                
    $CD $DLM-2;
                break;

            case 
    "Thursday":
                
    $CD $DLM-3;
                break;

            case 
    "Friday":
                
    $CD $DLM-4;
                break;

            case 
    "Saturday":
                
    $CD $DLM-5;
            }
            return 
    $CD;
        }

        
    /**
         * Add specified number of months to date.
         * 
         * This method adjusts the result to the final day of the month if
         * the resulting date is invalid, e.g., September 31 is converted
         * to September 30. Results in February also take account of leap
         * year. This contrasts with DateTime::modify() and strtotime, which
         * produce unexpected results by adding the day(s). 
         *
         * @param int $numMonths Number of months to be added.
         */
        
    public function addMonths($numMonths) {
            if (! 
    is_numeric $numMonths ) || $numMonths 1) {
                throw new 
    Exception 'addMonths() expects a positive integer.' );
            }
            
    $numMonths = ( int ) $numMonths;
            
    // Add the months to the current month number.
            
    $newValue $this->_month $numMonths;
            
    // If the new value is less than or equal to 12, the year
            // doesn't change, so just assign the new value to the month.
            
    if ($newValue <= 12) {
                
    $this->_month $newValue;
            } else {
                
    // A new value greater than 12 means calculating both
                // the month and the year. Calculating the year is
                // different for December, so do modulo division 
                // by 12 on the new value. If the remainder is not 0,
                // the new month is not December. 
                
    $notDecember $newValue 12;
                if (
    $notDecember) {
                    
    // The remainder of the modulo division is the new month.
                    
    $this->_month $notDecember;
                    
    // Divide the new value by 12 and round down to get the
                    // number of years to add.
                    
    $this->_year += floor $newValue 12 );
                } else {
                    
    // The new month must be December
                    
    $this->_month 12;
                    
    $this->_year += ($newValue 12) - 1;
                }
            }
            
    $this->checkLastDayOfMonth ();
            
    parent::setDate $this->_year$this->_month$this->_day );
        }
        
        
    /**
         * Subtract specified number of months from date.
         * 
         * This method adjusts the result to the final day of the month if
         * the resulting date is invalid, e.g., September 31 is converted
         * to September 30. Results in February also take account of leap
         * year. This contrasts with DateTime::modify() and strtotime, which
         * produce unexpected results by subtracting the day(s). 
         *
         * @param int $numMonths Number of months to be subtracted.
         */
        
    public function subMonths($numMonths) {
            if (! 
    is_numeric $numMonths )) {
                throw new 
    Exception 'addMonths() expects an integer.' );
            }
            
    $numMonths abs intval $numMonths ) );
            
    // Subtract the months from the current month number.
            
    $newValue $this->_month $numMonths;
            
    // If the result is greater than 0, it's still the same year,
            // and you can assign the new value to the month.
            
    if ($newValue 0) {
                
    $this->_month $newValue;
            } else {
                
    // Create an array of the months in reverse.
                
    $months range (121);
                
    // Get the absolute value of $newValue.
                
    $newValue abs $newValue );
                
    // Get the array position of the resulting month.
                
    $monthPosition $newValue 12;
                
    $this->_month $months [$monthPosition];
                
    // Arrays begin at 0, so if $monthPosition is 0,
                // it must be December.
                
    if ($monthPosition) {
                    
    $this->_year -= ceil $newValue 12 );
                } else {
                    
    $this->_year -= ceil $newValue 12 ) + 1;
                }
            }
            
    $this->checkLastDayOfMonth ();
            
    parent::setDate $this->_year$this->_month$this->_day );
        }

        
    /**
         * Create a month calendar.
         * 
         * This method returns a month grid calendar according to the 
         * workDate parameter
         *
         * @param array $events Array with events to be added to calendar.
         */
        
    protected function createMonth($events=array())
        {
    //        $this->_year = ( int ) date( 'Y', $this->_workDate);
    //        $this->_month = ( int ) date( 'n', $this->_workDate);
    //        $this->_day = ( int ) date( 'j', $this->_workDate);        

    //        echo '<h1>'.$this->_year.'-'.$this->_month.'-01</h1>';
    //        echo 'New next month: '.date('Y-m-d', strtotime($this->_year.'-'.$this->_month.'-01 +1 month'));

            
    $nextMonthDate date('Y-m-d'strtotime($this->_year.'-'.$this->_month.'-01 +1 month'));
            
    $previousMonthDate date('Y-m-d'strtotime($this->_year.'-'.$this->_month.'-01 -1 month'));

            
    $previousMonth date('m'strtotime($previousMonthDate));
            
    $nextMonth date('m'strtotime($nextMonthDate));

            
    $daysThisMonth date("t",strtotime(date("Y-m-d",strtotime($this->_workDate))));
            
    $daysLastMonth date("t",strtotime(date("Y-m-d"strtotime($previousMonth))));
            
    $this->_inThisMonth 0;
            
    $currentDate $this->GetStartingPoint($daysLastMonth);

            echo 
    '<h3>previous month: '.$previousMonth.'</h3>';
            echo 
    '<h3>next month: '.$nextMonth.'</h3>';
            echo 
    '<h3>days this month: '.$daysThisMonth.'</h3>';
            echo 
    '<h3>days last month: '.$daysLastMonth.'</h3>';
            echo 
    '<h3>current date: '.$currentDate.'</h3>';

            if(isset(
    $events)&&!is_array($events))
            {
                exit(
    '<p>Is not array inside createMonth function!</p>');
            }

    //        $output = '<p><a href="'.$_SERVER['PHP_SELF'].'?workDate='.$previousMonth.'">Previous Month</a> | <a href="'.$_SERVER['PHP_SELF'].'?workDate='.$nextMonth.'">Next Month</a></p>';
            
    $output '<p><a href="'.$_SERVER['PHP_SELF'].'?workDate='.$previousMonthDate.'">Previous Month</a> | <a href="'.$_SERVER['PHP_SELF'].'?workDate='.$nextMonthDate.'">Next Month</a></p>';

            
    $output .= '<table><caption>'.date('F Y',strtotime($this->_workDate)).'</caption>
                            <tr>
                            <th>Sunday</th>
                            <th>Monday</th>
                            <th>Tuesday</th>
                            <th>Wednesday</th>
                            <th>Thursday</th>
                            <th>Friday</th>
                            <th>Saturday</th>
                            </tr>'
    ;


            
    // what if you find a start date, the beginning of the calendar,
            // then you set the end date. you would just have to +1 day until
            // you hit the end date. that way, you always have the day you are
            // printing, instead of just looping a for loop
            
    for($x=1;$x<7;$x++)
            {
                
    $tdDay '<tr>';
                
    $tdContent '<tr>';
                for(
    $i=0;$i<7;$i++)
                {
                    
    // place something inside the tdContent td
                    // $tdContent .= date('m',strtotime($this->_workDate));
                    // $tdInside = date('Y',strtotime($this->_workDate)).'-'.date('m',strtotime($this->_workDate)).'-'.$currentDate;
                    // $tdInside = $currentDate;

                    
    $tdContent .= '<td ';
                    
    // not from this month
                    
    if($this->_inThisMonth==0)
                    {
                        
    $tdDay .= '<td class="notThisMonth">'.$currentDate++.'</td>';
                        
    $tdContent .= 'class="notThisMonth">';

                        if((
    $currentDate-1)>15)
                        {
                            if(
    date('m'strtotime($this->_workDate))=='01')
                            {
                                
    $previousMonthYear $this->_year-1;
                                
    $nextMonthYear $this->_year;
                            }
                            elseif(
    date('m'strtotime($this->_workDate))=='12')
                            {
                                
    $previousMonthYear $this->_year;
                                
    $nextMonthYear $this->_year+1;
                            }
                            else
                            {
                                
    $previousMonthYear $this->_year;
                                
    $nextMonthYear $this->_year;
                            }
                            
    $tdContent .= 'previous month<br />';
                            
    $tdContent .= $previousMonthYear.'-'.$previousMonth.'-'.($currentDate-1);
                        }
                        elseif((
    $currentDate-1)<15)
                        {
                            if(
    date('m'strtotime($this->_workDate))=='01')
                            {
                                
    $previousMonthYear $this->_year-1;
                                
    $nextMonthYear $this->_year;
                            }
                            elseif(
    date('m'strtotime($this->_workDate))=='12')
                            {
                                
    $previousMonthYear $this->_year;
                                
    $nextMonthYear $this->_year+1;
                            }
                            else
                            {
                                
    $previousMonthYear $this->_year;
                                
    $nextMonthYear $this->_year;
                            }
                            
    $tdContent .= 'next month<br />';
                            
    $tdContent .= $nextMonthYear.'-'.$nextMonth.'-'.($currentDate-1);
                        }

                        
    // add stuff inside day
                        // $tdContent .= ($currentDate-1).' | '.date('Y',strtotime($this->_workDate)).'-'.date("n",strtotime($this->_workDate)).'-'.($currentDate-1);
                    
    }
                    else
                    {
                        
    // today
                        
    if( $currentDate == date("j") && date("n",strtotime($this->_workDate)) == date("n") && date("y",strtotime($this->_workDate)) == date("y") )
                        {
                            
    $tdDay .= '<td class="today">'.$currentDate++.'</td>';
                            
    $tdContent .= 'class="today">';
                            
                            
    // add stuff inside day
                            // $tdContent .= date('Y',strtotime($this->_workDate)).'-'.date("n",strtotime($this->_workDate)).'-'.($currentDate-1);
                        
    }
                        
    // this month
                        
    else
                        {
                            
    $tdDay .= '<td>'.$currentDate++.'</td>';
                            
    $tdContent .= '>';

                            
    // add stuff inside day
                            // $tdContent .= date('Y',strtotime($this->_workDate)).'-'.date("n",strtotime($this->_workDate)).'-'.($currentDate-1);
                        
    }
                    }

                    
    // close tdContent td
                    
    $tdContent .= '</td>';                

                    
    // this month
                    
    if( $this->_inThisMonth == && $currentDate $daysLastMonth )
                    {
                        
    $currentDate 1;
                        
    $this->_inThisMonth 1;
                    }
                    
    // not this month
                    
    elseif($this->_inThisMonth == && $currentDate $daysThisMonth )
                    {
                        
    $currentDate 1;
                        
    $this->_inThisMonth 0;
                    }
        
                }
                
    $tdDay .= '</tr>';
                
    $tdContent .= '</tr>';
                
    $output .= $tdDay.$tdContent;
            }
            
    $output .= '</table>';
            return 
    $output;
        }

        public function 
    displayCalendar()
        {
            
    $calendarHeader '<p><a href="'.$_SERVER['PHP_SELF'].'?CalendarType=day&workDate='.$this->_workDate.'">day</a> | <a href="'.$_SERVER['PHP_SELF'].'?CalendarType=week&workDate='.$this->_workDate.'">week</a> | <a href="'.$_SERVER['PHP_SELF'].'?CalendarType=month&workDate='.$this->_workDate.'">month</a></p><p><a href="'.$_SERVER['PHP_SELF'].'?CalendarType='.$this->_calendarType.'&workDate='.date("Y-m-d").'">Today</a></p>';
            
    // change to a swich/case instead of ifelse
            
    if($this->_calendarType=='month')
            {
                return 
    $calendarHeader.$this->createMonth($this->_events);
            }
            elseif(
    $this->_calendarType=='week')
            {
                return 
    $calendarHeader.$this->createWeek($this->_workDateYMD);
            }
            elseif(
    $this->_calendarType=='day')
            {
                return 
    $calendarHeader.$this->createDay($this->_workDateYMD);
            }
        }

        
    /**
         * Determines whether the year is a leap year.
         *
         * @return bool True if it is a leap year, otherwise false.
         */
        
    public function isLeap() {
            if (
    $this->_year 400 == || ($this->_year == && $this->_year 100 != 0)) {
                return 
    true;
            } else {
                return 
    false;
            }
        }

        
    ###########################################################
        # PROTECTED METHOD                                        #
        ###########################################################
        /**
         * Adjusts the day to the last day of the month when necessary.
         * 
         * This internal method is called by addMonths(), subMonths(),
         * addYears(), and subYears(). It checks whether the date calculation
         * results in an invalid date, such as February 31. If the date is 
         * invalid, it readjusts the $_day property to the last day of
         * the month.
         */
        
    final protected function checkLastDayOfMonth() {
            if (! 
    checkdate $this->_month$this->_day$this->_year )) {
                
    $use30 = array (46911 );
                if (
    in_array $this->_month$use30 )) {
                    
    $this->_day 30;
                } else {
                    
    $this->_day $this->isLeap () ? 29 28;
                }
            }
        }
    }
    ?>
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  3. #3
    I know you guys probably don't have time to rewrite a class.

    At least, if you can give me some suggestions, point me to the right direction, so I know what I should do.

    I will greatly appreciate it.
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  4. #4
    A few remarks:


    • In your constructor, you're reading data from $_GET. This is generally pretty bad design because you're mixing concerns and relying on outside state in your objects. Your objects should be oblivious to any outside state. To solve this, don't get your data from $_GET but instead have the users of your class pass them as parameters. This allows users to pass in any data they want (and perform preprocessing on it at will) and keeps your objects free from outside concerns. This doesn't only apply to constructors btw.
    • I'm seeing a lot of pretty outdated functions being used, like date() and strtotime(). Many of the methods you've implemented are already available in standard PHP through the DateTime class (for example addMonths and subtractMonths (and lots more) are handled by DateTime::modify). I think you should re-evaluate the reason for writing this class and consider just using DateTime objects instead (or at least use them internally, since they perform a lot of the work you're doing manually right now).
    • I'm also seeing a lot of spaghetti hardcoded HTML output. I don't think I need to tell you why this is bad. For one, you're forcing each and every current and future user of your class to always have the same HTML output, regardless of their specific needs. What if I need to apply some classes to some of those tds? You should separate business logic from presentation and devise a way to enable users to use their own HTML if needed.
    Wait, what?

  5. #5
    Thanks!

    You're very right about the $_GET. In order for the user to navigate through the months, it needs a link that will send a URL query. So now, the class accepts on the constructor the value and it has to validate it before doing anything with it.

    You're right too, I should change to the object function of php DateTime. I will need to make some reading.

    About the HTML, well it has to output some HTML since it's creating a table. Now, maybe later I can add some functions to add the CSS instead of hardcoding it. The class has some CSS hooks and the styles can be applied in an outside file.

    PHP Code:
    class LeoCalendar
    {
        protected 
    $_workDate;
        protected 
    $_calendarType;
        protected 
    $_events;
        protected 
    $_year;
        protected 
    $_month;
        protected 
    $_day;

        
    // boolean
        // 0 = is not in this month
        // 1 = it is in this month
        
    protected $_inThisMonth;

        public function 
    __construct($date$calendarType$events=array())
        {
            if(!isset(
    $date))
            {
                
    $date date('Y-m-d');
                
    $this->_workDate $date;
            }
            else
            {
                
    $date date('Y-m-d'strtotime($date));
                
    $this->_workDate $date;
            }
            
    // assign the values to the class properties
            
    $this->_year = ( int ) date'Y'strtotime($this->_workDate));
            
    $this->_month = ( int ) date'n'strtotime($this->_workDate));
            
    $this->_day = ( int ) date'j'strtotime($this->_workDate));

            if(!isset(
    $calendarType))
            {
                
    $this->_calendarType 'month';
            }
            else
            {
                
    $isItAllowed 0;
                
    $allowedCalendarTypes = array('month''week''day');
                foreach(
    $allowedCalendarTypes as $key => $value)
                {
                    if(
    $value==$calendarType)
                    {
                        
    $isItAllowed 1;
                    }
                }
                if(
    $isItAllowed==1)
                {
                    
    $this->_calendarType $calendarType;
                }
                else
                {
                    
    $this->_calendarType='month';
                    
    // throw exception
                
    }
            }

            if(!
    is_null($events) && !is_array($events))
            {
                throw new 
    Exception('Must be array');
                exit(
    'Error!');
            }
            
    $this->_events $events;
        }

        
    ################################################
        # PUBLIC METHODS FOR blah blah blah            #
        ################################################
        
    public function createWeek($enterDate)
        {
            
    $today date('Y-m-d');
            
    $today_timestamp strtotime($today);

            
    $timestamp strtotime($enterDate);
            
    $timestamp_legible date('Y-m-d'$timestamp);

            if(
    date('l'$timestamp)=='Sunday')
            {
                
    $sunday_timestamp $timestamp;
                
    $sunday_legible date('F j, Y'$sunday_timestamp);
            }
            else
            {
                
    $sunday_timestamp strtotime(date('r',strtotime('last Sunday '.$timestamp_legible)));
                
    $sunday_legible date('F j, Y'$sunday_timestamp);
            }

            
    $year date('Y'$sunday_timestamp);

            
    $output '<p>
                            <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=week&workDate='.date('Y-m-d'strtotime('-1 day '.$sunday_legible)).'">Previous Week</a>
                             | 
                            <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=week&workDate='.date('Y-m-d'strtotime('+7 days '.$sunday_legible)).'">Next Week</a>
                        </p>
                        <table>
                            <caption>'
    .date('F j'$sunday_timestamp).' - '.date('F j, Y'strtotime('+6 days '.$sunday_legible)).'</caption>
                            <colgroup>
                                    <col id="time" />
                                    <col id="Sunday" />
                                    <col id="Monday" />
                                    <col id="Tuesday" />
                                    <col id="Wednesday" />
                                    <col id="Thursday" />
                                    <col id="Friday" />
                                    <col id="Saturday" />
                                </colgroup>
                                <thead><tr><th scope="col">'
    .$year.'</th>
                        '
    ;

            for(
    $i=0;$i<7;$i++)
            {
                
    $date date('l, M j'strtotime('+'.$i.' day '.$sunday_legible));
                
    $today_col '';
                if(
    strtotime($date) === $today_timestamp)
                {
                    
    $today_col 'id="today_col"';
                }
                else
                {
                    
    $today_col '';
                }

                
    $output .= '<th scope="col" '.$today_col.'>'.$date.'</th>';
            }
            
    $output .= '</tr></thead><tbody>';
        
            for(
    $i=6$i <= 22$i++)
            {
                
    $time strtotime('+'.$i.' hour 12AM');
                
    $hour date('H'$time);
                
    $time = ($hour != '12') ? date('g A'$time) : 'Noon';

                
    $output .= '<tr><th scope="row">'.$time.'</th>';
                for(
    $j=1$j<=7;$j++)
                {
                    
    $output .= '<td></td>';
                }
                
    $output .= '</tr>';
            }
            
    $output .= '</tbody></table>';
            return 
    $output;
        }

        public function 
    createDay($enterDate)
        {
            
    $timestamp strtotime($enterDate);
            
    $timestamp_legible date('Y-m-d'$timestamp);

            
    $output '<p>
                            <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=day&workDate='.date('Y-m-d'strtotime('-1 day '.$timestamp_legible)).'">Previous Day</a>
                             | 
                            <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=day&workDate='.date('Y-m-d'strtotime('+1 days '.$timestamp_legible)).'">Next Day</a>
                        </p>'
    ;

            
    $output .=    '<table>
                            <caption>'
    .date('F j'$timestamp).'</caption>
                            <thead><tr><th>'
    .date('Y'$timestamp).'</th><th>'.date('F j'$timestamp).'</th></tr></thead>
                            </tbody>
                        '
    ;

            for(
    $i=6$i <= 22$i++)
            {



                
    $time strtotime('+'.$i.' hour 12AM');
                
    $hour date('H'$time);
                
    $time = ($hour != '12') ? date('g A'$time) : 'Noon';

                
    $now '';

                if(
    $i==date('G') && $timestamp_legible==date('Y-m-d'))
                {
                    
    $now 'id="now"';
                }

                
    $output .= '<tr '.$now.'><th scope="row">'.$time.'</th><td></td>';
                
    $output .= '</tr>';
            }
            
    $output .= '</tbody></table>';
            return 
    $output;
        }

        protected function 
    GetStartingPoint($DLM)
        {
            
    $today getdate(strtotime($this->_workDate));
            
    $mday $today['mday'];
            
    $mday-=1;
            
    $FirstOfMonth date("Y-m-d",strtotime($this->_workDate "-" $mday " days"));

            switch(
    date("l",strtotime($FirstOfMonth)))
            {
            case 
    "Sunday":
                
    $CD 1;
                
    $this->_inThisMonth=1;
                break;

            case 
    "Monday":
                
    $CD $DLM;
                break;

            case 
    "Tuesday":
                
    $CD $DLM-1;
                break;

            case 
    "Wednesday":
                
    $CD $DLM-2;
                break;

            case 
    "Thursday":
                
    $CD $DLM-3;
                break;

            case 
    "Friday":
                
    $CD $DLM-4;
                break;

            case 
    "Saturday":
                
    $CD $DLM-5;
            }
            return 
    $CD;
        }
    /*
    protected function firstDayOfPreviousMonth($date)
    {
        $year = date('Y', strtotime($date));
        $month = date('m', strtotime($date));
        $day = date('d', strtotime($date));
        if($month==1)
        {
            $previousYear = $year-1;
            $previousMonth=12;
        }
        else
        {
            $previousYear = $year;
            $previousMonth=$month-1;
        }
        return date('Y-m-d', strtotime($previousYear.'-'.$previousMonth.'-01'));
    }

    protected function firstDayOfNextMonth($date)
    {
        $year = date('Y', strtotime($date));
        $month = date('m', strtotime($date));
        $day = date('d', strtotime($date));
        if($month==12)
        {
            $nextYear = $year+1;
            $nextMonth=1;
        }
        else
        {
            $nextYear = $year;
            $nextMonth=$month+1;
        }
        return date('Y-m-d', strtotime($nextYear.'-'.$nextMonth.'-01'));
    }
    */

        
    public function createMonth($date$events)
        {
        
            
    $legible_date date('l, F j,  Y'strtotime($date));
            
    $year date('Y'strtotime($date));
            
    $month date('m'strtotime($date));
            
    $day date('d'strtotime($date));
        
            
    // get first day of previous month
            // using the first day of the previous
            // month, the link to the previous month
            // is created
            
    if($month==1)
            {
                
    $previousYear $year-1;
                
    $previousMonth=12;
            }
            else
            {
                
    $previousYear $year;
                
    $previousMonth=$month-1;
            }
            
    // get first day of next month
            // using the first day of the next
            // month, the link to the next month
            // is created
            
    if($month==12)
            {
                
    $nextYear $year+1;
                
    $nextMonth=1;
            }
            else
            {
                
    $nextYear $year;
                
    $nextMonth=$month+1;
            }
        
            
    $firstDayOfMonth $year.'-'.$month.'-01';
            
    $firstDayOfPreviousMonth date('Y-m-d'strtotime($previousYear.'-'.$previousMonth.'-01'));
            
    $firstDayOfNextMonth date('Y-m-d'strtotime($nextYear.'-'.$nextMonth.'-01'));
            
    $numberOfDaysOfPreviousMonth date('t',strtotime(date('Y-m-d'strtotime($firstDayOfPreviousMonth))));
            
    $numberOfDaysOfCurrentMonth date('t',strtotime(date('Y-m-d'strtotime($year.'-'.$month.'-01'))));
        
            
    // what day of the week is the first day of the month?
            
    switch(date('l',strtotime($firstDayOfMonth)))
            {
                case 
    'Sunday':
                    
    $startTheCalendar 1;
                    break;
        
                case 
    'Monday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth;
                    break;
        
                case 
    'Tuesday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth-1;
                    break;
        
                case 
    'Wednesday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth-2;
                    break;
        
                case 
    'Thursday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth-3;
                    break;
        
                case 
    'Friday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth-4;
                    break;
        
                case 
    'Saturday':
                    
    $startTheCalendar $numberOfDaysOfPreviousMonth-5;
            }
        
            
    $calendar '<p><a href="'.$_SERVER['PHP_SELF'].'?workDate='.$firstDayOfPreviousMonth.'">&#x2190;</a> <a href="'.$_SERVER['PHP_SELF'].'?workDate='.$firstDayOfNextMonth.'">&#x2192;</a></p>';
            
    $calendar .= '<table><caption>'.date('F Y',strtotime($date)).'</caption>';
            
    $calendar .= '<tr>';
            
    $calendar .= '<th>Sunday</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th>';
            
    $calendar .= '</tr>';
            
            
    // does the calendar start with the previous month?
            
    if($startTheCalendar 20)
            {
                
    $startedPreviousMonth=1;
                
    $goCurrentMonth=0;
                
    $startedCurrentMonth=0;
            }
            else
            {
                
    $startedPreviousMonth=0;
                
    $goCurrentMonth=1;
                
    $startedCurrentMonth=1;
            }
            
            
    $days $startTheCalendar;
            
    $startedNextMonth=0;
            
    $goNextMonth=0;
            
            for(
    $d=1;$d<=42;$d++)
            {
                
    $css='';
                if(
    $d==or $d==or $d==15 or $d==22 or $d==29 or $d==36 or $d==43){$content.='<tr class="dayContent">';$day_label.='<tr class="dayLabel">';}
                if(
    $startedCurrentMonth==1)
                {
                    
    $startedPreviousMonth=0;
                    
    $startedCurrentMonth=0;
                    
    $days=1;
            
                    
    $c_year date('Y'strtotime($date));
                    
    $c_month date('m'strtotime($date));
            
                }
                elseif(
    $startedNextMonth==&& $days>15)
                {
                    
    $c_year $nextYear;
                    
    $c_month $nextMonth;
                    
    $days=1;
                }
            
                if(
    $startedPreviousMonth==1)
                {
                    
    $c_year $previousYear;
                    
    $c_month $previousMonth;
                    
    $css='class="previous"';
                }
                elseif(
    $startedNextMonth==1)
                {
                    
    $css='class="next"';
                }
                elseif(
    date('Y-m-j')==date('Y-m'strtotime($date)).'-'.$days)
                {
                    
    $css='id="today"';
                }

                
    $running_day $c_year.'-'.$c_month.'-'.$days;

                
    $day_content '';
                
                foreach(
    $events as $key => $value)
                {
                    if(
    $running_day==$value[0])
                    {
                        
    $day_content .= '<span>'.$value[1].'</span>';
                    }
                }

                
    $day_label .= '<td '.$css.'>'.date('j'strtotime($running_day)).'</td>';
                
    $content .= '<td '.$css.'>'.$day_content.'</td>';
            
                
    // display days of previous month
                
    if($startedPreviousMonth==&& $days<=$numberOfDaysOfPreviousMonth && $goCurrentMonth==0)
                {
                    if(
    $days==$numberOfDaysOfPreviousMonth)
                    {
                        
    $goCurrentMonth=1;
                        
    $startedCurrentMonth=1;
                    }
                }
                
    // display days of current month
                
    elseif($goCurrentMonth==1)
                {
                    if(
    $days==$numberOfDaysOfCurrentMonth)
                    {
                        
    $goCurrentMonth=2;
                        
    $goNextMonth=1;
                        
    $startedNextMonth=1;
                    }
                }
            
                if(
    $d==or $d==14 or $d==21 or $d==28 or $d==35 or $d==42)
                {
                    
    $content.='</tr>
                    '
    $day_label.='</tr>
                    '
    $rows.=$day_label.$content$content=''$day_label='';
                }
                
    $days++;
            }
            
    $calendar .= $rows.'</table>';
            return 
    $calendar;
        }

        public function 
    displayCalendar()
        {
            
    $calendarHeader =    '<p>
                                    <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=day&workDate='.$this->_workDate.'">day</a> | 
                                    <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=week&workDate='.$this->_workDate.'">week</a> | 
                                    <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType=month&workDate='.$this->_workDate.'">month</a>
                                </p>
                                <p>
                                    <a href="'
    .$_SERVER['PHP_SELF'].'?calendarType='.$this->_calendarType.'&workDate='.date("Y-m-d").'">Today</a>
                                </p>'
    ;
            
    // change to a swich/case instead of ifelse
            
    if($this->_calendarType=='month')
            {
                return 
    $calendarHeader.$this->createMonth($this->_workDate$this->_events);
            }
            elseif(
    $this->_calendarType=='week')
            {
                return 
    $calendarHeader.$this->createWeek($this->_workDate);
            }
            elseif(
    $this->_calendarType=='day')
            {
                return 
    $calendarHeader.$this->createDay($this->_workDate);
            }
        }

    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  6. #6
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    Default values are really helpful and more clean, especially in the constructor; then you don't have to use isset().

    I modified your constructor to be a bit more concise (haven't tested it, should work):
    PHP Code:
    public function __construct($date=""$calendarType="month"$events=array())
        {
            if(!isset(
    $date))
    {
        
    $date date('Y-m-d');
        
    $this->_workDate $date;
    }
    else
    {
        
    $date date('Y-m-d'strtotime($date));
        
    $this->_workDate $date;


            
    // assign the values to the class properties
            
    $this->_year = ( int ) date'Y'strtotime($this->_workDate));
            
    $this->_month = ( int ) date'n'strtotime($this->_workDate));
            
    $this->_day = ( int ) date'j'strtotime($this->_workDate));

            
    $this->_calendarType $calendarType;
            
    $allowedCalendarTypes = array('month''week''day');
            
            if(!
    in_array($this->_calendarType$allowCalendarTypes)){
                
    $this->_calendarType='month';
                
    // throw exception
            
    }

            if(!
    is_null($events) && !is_array($events))
            {
                throw new 
    Exception('Must be array');
                exit(
    'Error!');
            }
            
    $this->_events $events;
        } 
    Last edited by actionAction; November 9th, 2009 at 01:41 PM.

  7. #7
    Thanks! It did simplify the code. You're right, the foreach loop could easily be replaced by in_array. Sorry, I need to work on code simplification skills.

    Too complicated:
    PHP Code:
                if(!in_array($this->_calendarType$allowedCalendarTypes)){
                
    $this->_calendarType='month';
                
    // throw exception
                
    }


                foreach(
    $allowedCalendarTypes as $key => $value)
                {
                    if(
    $value==$calendarType)
                    {
                        
    $isItAllowed 1;
                    }
                }
                if(
    $isItAllowed==1)
                {
                    
    $this->_calendarType $calendarType;
                }
                else
                {
                    
    $this->_calendarType='month';
                    
    // throw exception
                

    Better:
    PHP Code:
            $this->_calendarType $calendarType;
            
    $allowedCalendarTypes = array('month''week''day');
            if(!
    in_array($this->_calendarType$allowedCalendarTypes)){
                
    $this->_calendarType='month';
                
    // throw exception
            

    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  8. #8
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012