Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I build a select box with dates?

Little tricks

How do I build a select box with dates?

by  missbarbell  Posted    (Edited  )
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-List/ or read the POD [2]
[2] http://search.cpan.org/dist/Calendar-List/List.pm


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;

# NOTE:
# '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);

# NOTE:
# 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";
$optcount++;
}
}

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

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

$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-Simple or read the POD [4]
[4] http://search.cpan.org/dist/Calendar-Simple/Simple.pm

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.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top