Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts

    generic calendar class

    PHP Code:
    class Generic_calendar {

        var 
    $_date;
        var 
    $_highlight;
        var 
    $_start_secs;
        var 
    $_total_days;
        var 
    $_month;
        var 
    $_month_text;
        var 
    $_year;


        function 
    Generic_calendar($date=0) {
            
    $this->_date =&$date;
            
    $this->_highlight = array();

            
    $this->_start_secs 0;
            
    $this->_total_days 0;

            
    $this->_month 0;
            
    $this->_month_text '';

            
    $this->_year  0;
        }

        function 
    highlight($days=array()) {
            
    $this->_highlight =&$days;
        }

        function 
    getPrevMonth() {
            return 
    strtotime('-1 month'$this->_date);
        }
        function 
    getNextMonth() {
            return 
    strtotime('+1 month'$this->_date);
        }

        function 
    getMonth() {
            if (
    $this->_month==0)
                
    $this->_month date('n'$this->_date);
            return 
    $this->_month;
        }
        
        function 
    getMonthText() {
            if (
    $this->_month_text=='')
                
    $this->_month_text date('M'$this->_date);
            return 
    $this->_month_text;
        }
        
        function 
    getYear() {
            if (
    $this->year==0)
                
    $this->_year date('Y'$this->_date);
            return 
    $this->_year;
        }

        function 
    getUnixStart() {
            if (
    $this->_start_secs==0)
                
    $this->_start_secs mktime(000$this->getMonth(), 0$this->getYear());
            return 
    $this->_start_secs;
        }

        function 
    getTotalDays() {
            if (
    $this->_total_days==0)
                
    $this->_total_days date('t'$this->_date);
            return 
    $this->_total_days; ;
        }

        function 
    getDays() {
            
    $start_secs $this->getUnixStart();
            
    $padding    date('w'$start_secs)+1;
            
    $total_days date('t'$this->_date);
            
            
    $days = array();

            
    $offset $padding;
            
    $padding_end $padding;
            
            if (
    $padding<7)
                while (
    $padding--)
                    
    $days[] = array( 'day' => 0);


            for (
    $x=1$x<=$total_days$x++) {
                
    $days[] = array(
                    
    'day' => $x,
                    
    'selected' => in_array$x$this->_highlight),
                    
    'new_week'  => $new_week = ($x==$offset) && ($offset += 7),
                    
    'timestamp' => $start_secs,
                );
                
    $start_secs += 86400;
                
            }
            
            while (
    $padding_end--)
                
    $days[] = array( 'day' => 0);

            return 
    $days;
        }

        function 
    getDaysOfWeek($offset=0) {
            return array(
                
    $offset => 'Sun'
                
    $offset => 'Mon'
                
    $offset => 'Tues'
                
    $offset => 'Wed'
                
    $offset => 'Thur'
                
    $offset => 'Fri'
                
    $offset => 'Sat'
            );
        }


    example usage:
    PHP Code:
        function dispCalendar($date, $days=array()) {

            $cal = new Generic_calendar($date);
            $cal->highlight($days);
            $days = $cal->getDays();
            //print_r($days);

            $next_month = $cal->getNextMonth();
            $prev_month = $cal->getPrevMonth();
            ?>
                <table id="calendar">
                <tr><td><a href="?date=<?php echo $prev_month?>">&lt;</a></td>
                <td colspan="5"><?php echo $cal->getMonthText().' '.$cal->getYear(); ?></td>
                <td><a href="?date=<?php echo $next_month?>">&gt;</a></td></tr>
                <tr>
            <?php
            
    foreach ($days as $day) {
                
                if (
    $day['new_week'] && $day['day']>1)
                    print 
    '</tr><tr>';

                
    $class $day['selected'] ? 'selected' 'normal';
                print 
    '<td class="'.$class.'">';
                    
                if (
    $day['day']==0) {
                    print 
    '&nbsp;';
                } else if (
    $day['selected']) {
                    print 
    '<a href="?date='.$day['timestamp'].'">'.$day['day'].'</a>';
                } else {
                    print 
    $day['day'];
                }

                print 
    '</td>';
            }
            print 
    '</tr></table>';
        }

    ?>
    <style type="text/css"><!--
    td { vertical-align: top; }
    #calendar { text-align: center; font-size: 8pt;  }
    #calendar .normal { }
    #calendar .selected { color: #ff0000; }
    #calendar a { color: #0000FF; }
    --></style>
    <?php

    if ($_GET['date']) {
        
    $date = (int)$_GET['date'];
    } else {
        
    $date time();
    }

    dispCalendar($date, array(5,6,7));

    ?>
    I like it when people tear my code apart if only to make me better.. so, feel free (and I know I should be commenting.. and I think the 'algorithm' I'm using for figuring out the start and end padding of a month is probably not so good).

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,048
    Thanks
    10
    Thanked 93 Times in 91 Posts
    Very nice , perfect for subclassing with more specific HTML/templated classes.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

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