Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 3 of 3

Thread: createMonth function - a different way to work the days

  1. #1

    createMonth function - a different way to work the days

    The events part doesn't work yet.

    The part I need help first is on the way the days are populated. I use a for loop but I think there might be a better way, getting the first day of the calendar and then probably adding +1 day using strtotime or something like that.

    I will continue tweaking it but if you guys have a recommendation please let me know.

    Thanks

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

            
    $output '<p><a href="'.$_SERVER['PHP_SELF'].'?workDate='.$this->_previousMonth.'">Previous Month</a> | <a href="'.$_SERVER['PHP_SELF'].'?workDate='.$this->_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>'
    ;
            for(
    $x=1;$x<7;$x++)
            {
                
    $tdDay '<tr>';
                
    $tdContent '<tr>';
                for(
    $i=1;$i<8;$i++)
                {
                    
    // place something inside the tdContent td
                    // $tdContent .= date('m',strtotime($this->_workDate));
                    // $tdInside = date('Y',strtotime($this->_workDate)).'-'.date('m',strtotime($this->_workDate)).'-'.$this->_currentDate;
                    // $tdInside = $this->_currentDate;

                    
    $tdContent .= '<td ';
                    
    // if it's not from this month
                    
    if($this->_inThisMonth==0)
                    {
                        
    $tdDay .= '<td class="notThisMonth">'.$this->_currentDate++.'</td>';
                        
    $tdContent .= 'class="notThisMonth">';
                    }
                    
    // days from this month
                    
    else
                    {
                        if( 
    $this->_currentDate == date("j") && date("n",strtotime($this->_workDate)) == date("n") && date("y",strtotime($this->_workDate)) == date("y") )
                        {
                            
    $tdDay .= '<td class="today">'.$this->_currentDate++.'</td>';
                            
    $tdContent .= 'class="today">';
                        }
                        else
                        {
                            
    $tdDay .= '<td>'.$this->_currentDate++.'</td>';
                            
    $tdContent .= '>';
                        }
                    }

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

                    if( 
    $this->_inThisMonth == && $this->_currentDate $this->_daysLastMonth )
                    {
                        
    $this->_currentDate 1;
                        
    $this->_inThisMonth 1;
                    }
                    elseif(
    $this->_inThisMonth == && $this->_currentDate $this->_daysThisMonth )
                    {
                        
    $this->_currentDate 1;
                        
    $this->_inThisMonth 0;
                    }
        
                }
                
    $tdDay .= '</tr>';
                
    $tdContent .= '</tr>';
                
    $output .= $tdDay.$tdContent;
            }
            
    $output .= '</table>';
            return 
    $output;
        } 
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  2. #2
    Corrected
    PHP Code:
    function createMonth($events=array()){
            
    $this->_previousMonth date("Y-m-d"strtotime($this->_workDate "-1 month"));
            
    $this->_nextMonth date("Y-m-d"strtotime($this->_workDate "+1 month"));
            
    $this->_daysThisMonth date("t",strtotime(date("Y-m-d",strtotime($this->_workDate))));
            
    $this->_daysLastMonth date("t",strtotime(date("Y-m-d"strtotime($this->_previousMonth))));
            
    $this->_inThisMonth 0;
            
    $this->_currentDate $this->GetStartingPoint($this->_daysLastMonth);

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

            
    $output '<p><a href="'.$_SERVER['PHP_SELF'].'?workDate='.$this->_previousMonth.'">Previous Month</a> | <a href="'.$_SERVER['PHP_SELF'].'?workDate='.$this->_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>'
    ;
            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)).'-'.$this->_currentDate;
                    // $tdInside = $this->_currentDate;

                    
    $tdContent .= '<td ';
                    
    // if it's not from this month
                    
    if($this->_inThisMonth==0)
                    {
                        
    $tdDay .= '<td class="notThisMonth">'.$this->_currentDate++.'</td>';
                        
    $tdContent .= 'class="notThisMonth">';
                    }
                    
    // days from this month
                    
    else
                    {
                        if( 
    $this->_currentDate == date("j") && date("n",strtotime($this->_workDate)) == date("n") && date("y",strtotime($this->_workDate)) == date("y") )
                        {
                            
    $tdDay .= '<td class="today">'.$this->_currentDate++.'</td>';
                            
    $tdContent .= 'class="today">';
                        }
                        else
                        {
                            
    $tdDay .= '<td>'.$this->_currentDate++.'</td>';
                            
    $tdContent .= '>';
                        }
                    }

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

                    if( 
    $this->_inThisMonth == && $this->_currentDate $this->_daysLastMonth )
                    {
                        
    $this->_currentDate 1;
                        
    $this->_inThisMonth 1;
                    }
                    elseif(
    $this->_inThisMonth == && $this->_currentDate $this->_daysThisMonth )
                    {
                        
    $this->_currentDate 1;
                        
    $this->_inThisMonth 0;
                    }
        
                }
                
    $tdDay .= '</tr>';
                
    $tdContent .= '</tr>';
                
    $output .= $tdDay.$tdContent;
            }
            
    $output .= '</table>';
            return 
    $output;
        } 
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

  3. #3
    Modified more. Will keep on working on it tomorrow.
    PHP Code:
        function createMonth($events=array())
        {
            
    $previousMonth date("Y-m-d"strtotime($this->_workDate "-1 month"));
            
    $nextMonth date("Y-m-d"strtotime($this->_workDate "+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);

            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 .= '<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 ';
                    
    // if it's not from this month
                    
    if($this->_inThisMonth==0)
                    {
                        
    $tdDay .= '<td class="notThisMonth">'.$currentDate++.'</td>';
                        
    $tdContent .= 'class="notThisMonth">';
                    }
                    
    // days from this month
                    
    else
                    {
                        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">';
                        }
                        else
                        {
                            
    $tdDay .= '<td>'.$currentDate++.'</td>';
                            
    $tdContent .= '>';
                        }
                    }

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

                    if( 
    $this->_inThisMonth == && $currentDate $daysLastMonth )
                    {
                        
    $currentDate 1;
                        
    $this->_inThisMonth 1;
                    }
                    elseif(
    $this->_inThisMonth == && $currentDate $daysThisMonth )
                    {
                        
    $currentDate 1;
                        
    $this->_inThisMonth 0;
                    }
        
                }
                
    $tdDay .= '</tr>';
                
    $tdContent .= '</tr>';
                
    $output .= $tdDay.$tdContent;
            }
            
    $output .= '</table>';
            return 
    $output;
        } 
    "You are playing a very dangerous game"
    "Change always is"

    Creativo Development

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