Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Little tricks

How do I build a select box with dates? by missbarbell
Posted: 25 Apr 03 (Edited 24 Sep 03)

Earlier this year someone posted a question regarding dates in a select box ... Thread219-532164

Originally I wrote a simple code snippet in this FAQ explaining a very simple way of doing it (see below for the original text). However, since then I have released a drastically improved version to CPAN, entitled Calendar::List [1], which does all the original poster wanted and a whole lot more.

[1] http://search.cpan.org/dist/Calendar-Lis... or read the POD [2]
[2] http://search.cpan.org/dist/Calendar-Lis...

The problem

Suppose you need the next 30 days (from the current date), excluding weekends, to be listed in a SELECT tag for use in an HTML FORM. You could cycle round the localtime/time functions, but you would need to do alot of checking to ensure you got the right dates, etc.

Using Calendar::List

#!/usr/bin/perl -w
use strict;

# load the CPAN module
use Calendar::List;

# 'options' is set at 30 if the entry is missing
# 'exclude' can use 'weekdays' or any of the named days of the week
# 'name' defaults to "calendar"

my %options = (
    'options'       => 30,
    'exclude'       => { 'weekends' => 1 },
        'name'          => 'MyDates',

my $select = calendar_selectbox('DD-MM-YYYY',\%options);
print $select;

For further options, eg specifying start, end or selected dates, have a look at the POD document for the module [2]. This module does require a couple of additional modules, Clone and Tie::IxHash.

Using Calendar::Simple

Another way is to use the CPAN module Calendar::Simple as shown below:

#!/usr/bin/perl -w
use strict;

# load the CPAN module
use Calendar::Simple;

# prime our print out names
my @months = qw(    NULL January February March April May June July
                    August September October November December );

# get the current date
my @now = localtime();
my $nowday    = $now[3];                    # store todays day number
my $nowmon    = $now[4]+1;                # store todays month number
my $nowyear    = $now[5] + 1900;            # store todays year number (YYYY)

my $optcount = 0;                        # our option counter
my $maxcount = 30;                        # our option limit

my $select = "<select name='dates'>\n";    # open SELECT tag

while($optcount < $maxcount) {
    # get the calendar for one month
    my @this_month = calendar($nowmon,$nowyear);

# calendar() returns a list of weeks, which reference 7 days of a single
# calendar line, starting from Sunday, ending on Saturday. If an element
# is blank either the month starts midweek, or ends midweek.

    foreach my $week (@this_month) {
        last    if($optcount > $maxcount);    # end if we have enough options

        # ignore weekends
        shift @$week;                        # ignore sundays
        pop @$week;                            # ignore saturdays

        foreach my $day (@$week) {
            # ignore blanks, and days prior to start date
            next    unless($day);
            next    unless($optcount || $day >= $nowday);

            # format date strings
            my $date1 = sprintf "%04d-%02d-%02d",
                        $nowyear,$nowmon,$day;                # YYYY-MM-DD
            my $date2 = "$day $months[$nowmon], $nowyear";    # DD MONTH, YYYY

            # add an OPTION element
            $select .=  "<option value='$date1'>$date2</option>\n";

    next    if($optcount > $maxcount);    # end if we have enough options

    # increment to next month (and year if applicable)
    if($nowmon > 12) {
        $nowmon = 1;

$select .= "<select>\n";                # close SELECT tag

print $select;

Have a look at Calendar::Simple [3] on CPAN for further info. Though be aware that if you do use the module, you'll have to install a few of the Test and IO modules.

[3] http://search.cpan.org/dist/Calendar-Sim... or read the POD [4]
[4] http://search.cpan.org/dist/Calendar-Sim...

You could of course modify the code to include weekends (by removing the ignore weekend lines) or extending the maximum number of options you (by changing $maxcount) or starting on a different date (by passing localtime() the date you want to start) or change the format the dates are returned in. Your choices are endless, but the above code is a good place to start.

This FAQ was inspired by a thread on date printing Thread219-532164.

Back to Perl FAQ Index
Back to Perl Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close