Help!
I am trying to implement "Due Date" fields in my database that are automatically set to be 15 days after a user entered date "Start" date. The "Start" dates may not be entered at the time the record is created but may be entered later on during record modification.
I assume I need to write a function that turns the "Start" date to UNIX format then adds 1296000s (15 days), then reconverts the UNIX date to a real date, and finally enters the result in the proper field. This function needs to be called by both &add_record and &modify_record. I have tried the following, but they don't seem to work:
ADDITION TO &add_record
# Check to see if a Date_Sent Field is filled in and then Calculate Corresponding Due Date
if ($db_Date_Sent_R1) {$db_Date_Due_R1 = &due_date_calc($db_Date_Sent_R1);}
if ($db_Date_Sent_R2) {$db_Date_Due_R2 = &due_date_calc($db_Date_Sent_R2);}
OR
if ($in{7}) {$in{9} = &due_date_calc($in{7});}
if ($in{8}) {$in{10} = &due_date_calc($in{8});}
THE SUBROUTINE
sub due_date_calc {
# --------------------------------------------------------
# This routine adds 15 days a date.
my ($date) = $_[0];
my (%months) = ("Jan" => 0, "Feb" => 1, "Mar" => 2, "Apr" => 3, "May" => 4, "Jun" => 5,
"Jul" => 6, "Aug" => 7, "Sep" => 8, "Oct" => 9, "Nov" => 10,"Dec" => 11);
my ($time);
my ($day, $mon, $year) = split(/-/, $_[0]);
unless ($day and $mon and $year) { return undef; }
unless (defined($months{$mon})) { return undef; }
use Time::Local;
eval {
$day = int($day); $year = int($year) - 1900;
$time = timelocal(0,0,0,$day, $months{$mon}, $year);
$time = $time + 1296000;};
if ($@) { return undef; } # Could return 0 if you want.
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time);
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;
return "$day-$months[$mon]-$year";
}
Please help this PERL neophyte!
Sincerely,
Lauren Stegman
I am trying to implement "Due Date" fields in my database that are automatically set to be 15 days after a user entered date "Start" date. The "Start" dates may not be entered at the time the record is created but may be entered later on during record modification.
I assume I need to write a function that turns the "Start" date to UNIX format then adds 1296000s (15 days), then reconverts the UNIX date to a real date, and finally enters the result in the proper field. This function needs to be called by both &add_record and &modify_record. I have tried the following, but they don't seem to work:
ADDITION TO &add_record
# Check to see if a Date_Sent Field is filled in and then Calculate Corresponding Due Date
if ($db_Date_Sent_R1) {$db_Date_Due_R1 = &due_date_calc($db_Date_Sent_R1);}
if ($db_Date_Sent_R2) {$db_Date_Due_R2 = &due_date_calc($db_Date_Sent_R2);}
OR
if ($in{7}) {$in{9} = &due_date_calc($in{7});}
if ($in{8}) {$in{10} = &due_date_calc($in{8});}
THE SUBROUTINE
sub due_date_calc {
# --------------------------------------------------------
# This routine adds 15 days a date.
my ($date) = $_[0];
my (%months) = ("Jan" => 0, "Feb" => 1, "Mar" => 2, "Apr" => 3, "May" => 4, "Jun" => 5,
"Jul" => 6, "Aug" => 7, "Sep" => 8, "Oct" => 9, "Nov" => 10,"Dec" => 11);
my ($time);
my ($day, $mon, $year) = split(/-/, $_[0]);
unless ($day and $mon and $year) { return undef; }
unless (defined($months{$mon})) { return undef; }
use Time::Local;
eval {
$day = int($day); $year = int($year) - 1900;
$time = timelocal(0,0,0,$day, $months{$mon}, $year);
$time = $time + 1296000;};
if ($@) { return undef; } # Could return 0 if you want.
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time);
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;
return "$day-$months[$mon]-$year";
}
Please help this PERL neophyte!
Sincerely,
Lauren Stegman