Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Looking for easy way to generate html calendars

by twotone (Beadle)
on Nov 19, 2006 at 11:52 UTC ( #584942=perlquestion: print w/replies, xml ) Need Help??

twotone has asked for the wisdom of the Perl Monks concerning the following question:

I want to generate html calendars easily with the ability to customize the appearance. I need to place 1 or more live show events on certain days linked to a "buy tickets" page.

Is HTML::CalendarMonth my best option, or is there an easier or better way? And does anyone know of an in depth tutorial for HTML::CalendarMonth, or for whichever other solution you might suggest?
  • Comment on Looking for easy way to generate html calendars

Replies are listed 'Best First'.
Re: Looking for easy way to generate html calendars
by jonadab (Parson) on Nov 19, 2006 at 13:02 UTC
    I want to generate html calendars

    I actually have code that does this, which I use to generate study calendars for a quiz team I coach. Hmmm..., come to think of it, I also have code elsewhere that generates HTML calendars for other purposes. It's not hard.

    with the ability to customize the appearance.

    Customize the appearance using CSS.

    Is HTML::CalendarMonth my best option

    I don't know. I always just use DateTime myself. It's not like generating a calendar is hard...

    update: factored out the file operations and the rest of the HTML page, so that the subroutine just returns a calendar.
    sub html_calendar { my ($month, $year, $eventshashref) = @_; my $dt = DateTime->new(year => $year, month => $month, day => 1, ); my $calendar = qq[ <table class="calendarmonth"><thead> <tr><th colspan="7" class="monthname">] . $dt->month_name . qq[ $yea +r</th></tr> <tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th> +Fri</th><th>Sat</th></tr> </thead><tbody> <tr>]; $calendar .= '<td class="othermonth">&nbsp;</td>' for 1 .. ($dt->dow + % 7); while ($dt->month == $month) { $calendar .= qq[<td><span class="daynum">] . $dt->mday . '</span>' +; if (ref $eventshashref and exists $$eventshashref{$dt->mday}) { my $class = $$eventshashref{$dt->mday()}{class}; my $time = $$eventshashref{$dt->mday()}{time}; my $desc = $$eventshashref{$dt->mday()}{shortdesc}; my $tooltip = $$eventshashref{$dt->mday()}{longdesc}; $calendar .= qq[<div class="calendarevent $class"><span class="e +venttime">$time</span> <abbr title="$tooltip">$desc</abbr></div>]; } $calendar .= qq[</td>]; $dt = $dt->add(days => 1); if ($dt->dow == 7) { $calendar .= "</tr>\n <tr>", } } $calendar .= qq[</tr>\n</tbody></table>]; return $calendar; }

    Call it thusly:

    my $cal = html_calendar(11, 2006, { 23 => {class => 'holiday', time => '', shortdesc => 'Thanksgiving', longdesc => 'Stay home and stuff yourself.'}});


    Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. You can just call me "Mister Sanity". Why, I've got so much sanity it's driving me crazy.
Re: Looking for easy way to generate html calendars
by zentara (Archbishop) on Nov 19, 2006 at 13:39 UTC
    Here are a couple of others.
    #!/usr/bin/perl use CGI qw(header); use HTML::CalendarMonthSimple; use strict; my %cal; while (<DATA>) { chomp; my ($file,$yyyy,$mm,$dd) = /([^.]+)\.(\d{4})(\d\d)(\d\d)/; push @{$cal{$yyyy}->{$mm}},{day => $dd, targ => $file}; } print header; for my $year (sort keys %cal) { for my $mon (sort keys %{$cal{$year}}) { my $cal = HTML::CalendarMonthSimple->new( month => $mon, year => $year, ); for (sort @{$cal{$year}->{$mon}}) { $cal->setdatehref(int($_->{day}), $_->{targ}); } print $cal->as_HTML(); } } __DATA__ file5.20030227 file2.20020802 file1.20020801 file4.20021015 file3.20020803
    #!/usr/bin/perl -w use strict; use CGI; use Calendar::Simple; # HTML::CalendarMonthSimple seems even better suited # HTML::CalendarMonthSimple is a Perl module for # generating, manipulating, and printing a HTML calendar # grid for a specified month. It is intended as a faster # and easier-to-use alternative to HTML::CalendarMonth." my $rqry = new CGI(); print CGI::header(); my @months = qw(January February March April May June July August September October November December); my $mon = shift || (localtime)[4] + 1; my $yr = shift || ((localtime)[5] + 1900); my @month = calendar($mon, $yr); print '<html><body>'; print "$months[$mon -1] $yr\n\n"; print '<table>'; print '<tr><td>Su</td> <td>Mo</td> <td>Tu</td> <td>We</td>' .'<td>Th</td> <td>Fr</td> <td>Sa</td></tr></tr>'; foreach (@month) { print '<tr>'; print map{"<td>$_</td>"} @$_; print '</tr>'; } print "</table>"; print "<pre>// or with pre-tag:\n"; print "$months[$mon -1] $yr\n"; print "Su Mo Tu We Th Fr Sa\n"; foreach (@month) { print map { $_ ? sprintf("%2d ", $_) : ' ' } @$_; print "\n"; } print '</pre></body></html>';

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://584942]
Approved by andyford
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2019-12-06 20:45 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (159 votes). Check out past polls.