• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

PHP calendar problem

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
hnmcc
Asked:
hnmcc
2 Solutions
 
Ray PaseurCommented:
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
 
shefenCommented:
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
 
hnmccAuthor Commented:
Shefen answered my query exactly, Ray's suggested reading did help me to understand the issues better.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now