• Event Calendar with PHP script

     

    Event Calendar with PHP script

    We can create event calendar with php script. Here i have defined php script for the event calendar. You can get highlight on event added date in calendar.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    
    <!--?php
    class Calendar
    {
        var $events;
     
        function Calendar($date)
        {
            if(empty($date)) $date = time();
            define('NUM_OF_DAYS', date('t',$date));
            define('CURRENT_DAY', date('j',$date));
            define('CURRENT_MONTH_A', date('F',$date));
            define('CURRENT_MONTH_N', date('n',$date));
            define('CURRENT_YEAR', date('Y',$date));
            define('START_DAY', (int) date('N', mktime(0,0,0,CURRENT_MONTH_N,1, CURRENT_YEAR)) - 1);
            define('COLUMNS', 7);
            define('PREV_MONTH', $this--->prev_month());
            define('NEXT_MONTH', $this-&gt;next_month());
            $this-&gt;events = array();
        }
     
        function prev_month()
        {
            return mktime(0,0,0,
                    (CURRENT_MONTH_N == 1 ? 12 : CURRENT_MONTH_N - 1),
                    (checkdate((CURRENT_MONTH_N == 1 ? 12 : CURRENT_MONTH_N - 1), CURRENT_DAY, (CURRENT_MONTH_N == 1 ? CURRENT_YEAR - 1 : CURRENT_YEAR)) ? CURRENT_DAY : 1),
                    (CURRENT_MONTH_N == 1 ? CURRENT_YEAR - 1 : CURRENT_YEAR));
        }
     
        function next_month()
        {
            return mktime(0,0,0,
                    (CURRENT_MONTH_N == 12 ? 1 : CURRENT_MONTH_N + 1),
                    (checkdate((CURRENT_MONTH_N == 12 ? 1 : CURRENT_MONTH_N + 1) , CURRENT_DAY ,(CURRENT_MONTH_N == 12 ? CURRENT_YEAR + 1 : CURRENT_YEAR)) ? CURRENT_DAY : 1),
                    (CURRENT_MONTH_N == 12 ? CURRENT_YEAR + 1 : CURRENT_YEAR));
        }
     
        function getEvent($timestamp)
        {
            $event = NULL;
            if(array_key_exists($timestamp, $this-&gt;events))
                $event = $this-&gt;events[$timestamp];
            return $event;
        }
     
        function addEvent($event, $day = CURRENT_DAY, $month = CURRENT_MONTH_N, $year = CURRENT_YEAR)
        {
            $timestamp = mktime(0, 0, 0, $month, $day, $year);
            if(array_key_exists($timestamp, $this-&gt;events))
                array_push($this-&gt;events[$timestamp], $event);
            else
                $this-&gt;events[$timestamp] = array($event);
        }
     
        function makeEvents()
        {
            if($events = $this-&gt;getEvent(mktime(0, 0, 0, CURRENT_MONTH_N, CURRENT_DAY, CURRENT_YEAR)))
                foreach($events as $event) echo $event.'';
        }
     
        function makeCalendar()
        {
            echo '
    ';
            echo '
     
    ';
            echo '
     
    ';
            echo '
     
    ';
            echo '';
            echo '
     
    ';
            echo '
     
    ';
            echo '
     
    ';
            echo '
     
    ';
            echo '
     
    ';
            echo '
     
    ';
            echo '
     
    ';
            echo '';
     
            echo str_repeat('
     
    ', START_DAY);
     
            $rows = 1;
     
            for($i = 1; $i &lt;= NUM_OF_DAYS; $i++)
            {
                if($i == CURRENT_DAY)
                    echo '
     
    ';
                else if($event = $this-&gt;getEvent(mktime(0, 0, 0, CURRENT_MONTH_N, $i, CURRENT_YEAR)))
                    echo '
     
    ';
                else
                    echo '
     
    ';
     
                if((($i + START_DAY) % COLUMNS) == 0 &amp;&amp; $i != NUM_OF_DAYS)
                {
                    echo '';
                    $rows++;
                }
            }
            echo str_repeat('
     
    ', (COLUMNS * $rows) - (NUM_OF_DAYS + START_DAY)).'
    << ‘.CURRENT_MONTH_A .’ – ‘. CURRENT_YEAR.’ >>
    Mon Tue Wed Thu Fri Sat Sun
    ‘.$i.’ ‘.$i.’ ‘.$i.’
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    ';
        }
    }
     
    @$cal = new Calendar($_GET['date']);
    $cal-&gt;addEvent('event 1', 16, 7, 13); // You Can add event date here Right now we have added event at 16 July 2013
    $cal-&gt;makeCalendar();
    $cal-&gt;makeEvents();
    ?&gt;
    Tags:

Comments on this post

Leave a Reply

  • Security Code :


    7 − four =