PDA

View Full Version : Date & Working days


dav24
07-05-2005, 01:04 PM
Hi There,

I've got a problem to generate a date + 3 working days(Mon to Fri).

My code is generating a date like this:

$date = sprintf "%04d%02d%02d",
localtime->year +1900,
localtime->mon + 1,
localtime->mday;

The output will be YYYYMMDD

I would like to generate a second date which is $date + 3 working days. How can i do that? The problem is to avoid adding the weekend.

Thanks to anyone that can help me.
Dav24

mlseim
07-05-2005, 02:08 PM
dav24,

Dates are hard to work with ... fortunately, there is a module that does a ton of date calculations - probably more than you need to do, but maybe it will help you out.

http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod

Scroll down to some of the examples they have on doing some business day calculations.

Your webhost may already have the module installed. If not, you can put the module in your cgi-bin and utilize it from there ... basically, you're using it sort of like a subroutine, calling it for various calculations, etc.

If the module is really over-kill for you, let us know ... but it's a nice module to have on hand.

dav24
07-06-2005, 12:29 PM
Thanks for your reply.
I've been said about the Date::business module designed for calculation with business days.Do you know about it?
Knowing that i have a $date which is today's date,what would be the syntax to get $date +3 business days?
dav24

mlseim
07-06-2005, 08:35 PM
Because my webhost does not have that module installed, I had to put a copy into my cgi-bin (calling it "Business.pm"). See the code below that takes today's date (default since it's not specified) and adds an offset of 3, which pushes it out 3 business days.

Today is: 20050706 (which is Wednesday).
Printout shows: 20050711 (which is Monday, 3 business days).

If you would rather it return the value in UNIX time instead of YYYYMMDD,
simple change $d->image to $d->value.

#!/usr/bin/perl

use Business;

print "Content-type: text/html\n\n";

my $d = Business->new(OFFSET => 3); # today + 3 business days.

$f = $d->image; #format to YYYYMMDD

print $f;

Here's a fancier one with a $date variable and day-of-week indicator:

#!/usr/bin/perl

use Business;

@daynames=("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

print "Content-type: text/html\n\n";

$date = "20050805";

my $t = Business->new(DATE => $date);
my $d = Business->new(DATE => $date, OFFSET => 3); # $date + 3 business days.

my $f = $d->image; #format to YYYYMMDD

print "Original Date: $date, which is a $daynames[$t->day_of_week] <br><br> \n";
print "3 Business Days: $f, which is a $daynames[$d->day_of_week] <br><br> \n";

The printout looks like this:

Original Date: 20050805, which is a Friday

3 Business Days: 20050810, which is a Wednesday


.