Solved

PHP calendar problem

Posted on 2011-03-20
3
230 Views
Last Modified: 2012-05-11
I need to create a page showing an Outlook-style calendar - month view, each day in its own box with entries drawn from a database of events.

I have looked at Active Calendar, which is all that I want and far more - but too complicated for someone like me to insert my own data day-by-day - I don't understand the code well enough.

I want to look at the pc_calendar() function in PHP Cookbook, which I bought for the purpose: trouble is, I don't understand how the two part work together.  Probably it's obvious and it's only exhaustion that stops me from seeing it: but I can't.

The first part is this:
style type="text/css">
.prev { text-align: left; }
.next { text-align: right; }
.day, .month, .weekday { text-align: center; }
.today { background: yellow; }
.blank { }
</style>
<?php
// print the calendar for the current month if a month
// or year isn't in the query string
$month = isset($_GET['month']) ? intval($_GET['month']) : date('m');
$year = isset($_GET['year']) ? intval($_GET['year']) : date('y');
?>

Open in new window

The text says that this "shows how you can use the function."

And the main part is this:
?php
function pc_calendar($month,$year,$opts = '') {
    // set default options
    if (! is_array($opts)) { $opts = array(); }
    if (! isset($opts['id'])) { $opts['id'] = 'calendar'; }
    if (! isset($opts['month_link'])) {
        $opts['month_link'] = 
            '<a href="'.$_SERVER['PHP_SELF'].'?month=%d&amp;year=%d">%s</a>';
    }
    $classes = array();
    foreach (array('prev','month','next','weekday','blank','day','today') as $class) {
        if (isset($opts[$class.'_class'])) {
            $classes[$class] = htmlentities($opts[$class.'_class']);
        } else {
            $classes[$class] = $class;
        }
    }
    
    list($this_month,$this_year,$this_day) = split(',',strftime('%m,%Y,%d'));
    $day_highlight = (($this_month == $month) && ($this_year == $year));
    
    list($prev_month,$prev_year) = 
        split(',',strftime('%m,%Y',mktime(0,0,0,$month-1,1,$year)));
    $prev_month_link = sprintf($opts['month_link'],$prev_month,$prev_year,'&laquo;');
    
    list($next_month,$next_year) = 
        split(',',strftime('%m,%Y',mktime(0,0,0,$month+1,1,$year)));
    $next_month_link = sprintf($opts['month_link'],$next_month,$next_year,'&raquo;');
    
?>
<table id="<?php echo htmlentities($opts['id']) ?>">
        <tr>
                <td class="<?php echo $classes['prev'] ?>">
                        <?php print $prev_month_link ?>
                </td>
                <td class="<?php echo $classes['month'] ?>" colspan="5">
                <?php print strftime('%B %Y',mktime(0,0,0,$month,1,$year)); ?>
                </td>
                <td class="<?php echo $classes['next'] ?>">
                        <?php print $next_month_link ?>
                </td>
        </tr>
<?php
    $totaldays = date('t',mktime(0,0,0,$month,1,$year));
 
    // print out days of the week
    print '<tr>';
    $weekdays = array('Su','Mo','Tu','We','Th','Fr','Sa');
    while (list($k,$v) = each($weekdays)) {
        print '<td class="'.$classes['weekday'].'">'.$v.'</td>';
    }
    print '</tr><tr>';
    // align the first day of the month with the right week day
    $day_offset = date("w",mktime(0, 0, 0, $month, 1, $year));
    if ($day_offset > 0) { 
        for ($i = 0; $i < $day_offset; $i++) { 
            print '<td class="'.$classes['blank'].'">&nbsp;</td>';
        }
    }
    $yesterday = time() - 86400; 

    // print out the days
    for ($day = 1; $day <= $totaldays; $day++) {
        $day_secs = mktime(0,0,0,$month,$day,$year);
        if ($day_secs >= $yesterday) {  
            if ($day_highlight && ($day == $this_day)) {
                print '<td class="' . $classes['today'] .'">' . $day . '</td>';
            } else {
                print '<td class="' . $classes['day'] .'">' . $day . '</td>';
            }
        } else {
            print '<td class="' . $classes['day'] .'">' . $day .'</td>';
        }
        $day_offset++;

        // start a new row each week // 
        if ($day_offset == 7) {
            $day_offset = 0;
            if ($day < $totaldays) { print "</tr>\n<tr>"; }
        }
    }
    // fill in the last week with blanks //
    if ($day_offset > 0) { $day_offset = 7 - $day_offset; }
    if ($day_offset > 0) { 
        for ($i = 0; $i < $day_offset; $i++) { 
            print '<td class="'.$classes['blank'].'">&nbsp;</td>';
        }
    }
    print '</tr></table>';
}
?>

Open in new window

Obviously, I can understand much of what the function does: but I've become terminally confused about how to call it, and where the first part comes in.  And I'm working to a deadline, so I can't do something else and come back refreshed in a day or two, only to see it immediately!

So: how DO they fit together? And how would I call the function as-is in a real page?

Or, if you know of a good alternative - clear but not too simple - I'd like to hear about it.
0
Comment
Question by:hnmcc
3 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 35177158
You might find some useful information in this article.  See if it helps increase your understanding of how DATETIME information works in PHP and MySQL.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
 
LVL 1

Assisted Solution

by:shefen
shefen earned 250 total points
ID: 35180710
You can put them all together in one HTML page or you can separate them.

I would put the calendar function (your second block of code) in it's own file, called f_calendar.php then include that file in another file that is mostly HTML:

<?php 
  include 'f_calendar.php';
  // print the calendar for the current month if a month
  // or year isn't in the query string
  $month = isset($_GET['month']) ? intval($_GET['month']) : date('m');
  $year = isset($_GET['year']) ? intval($_GET['year']) : date('y');
?>
<html>
<head>
<style type="text/css">
.prev { text-align: left; }
.next { text-align: right; }
.day, .month, .weekday { text-align: center; }
.today { background: yellow; }
.blank { }
</style>
</head>
<body>
<?php pc_calendar($month, $year, $options); ?>
</body>
</html>

Open in new window


Note the call to pc_calendar() in the body of the html page.
0
 

Author Closing Comment

by:hnmcc
ID: 35191502
Shefen answered my query exactly, Ray's suggested reading did help me to understand the issues better.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question