Gossamer Forum
Home : General : Perl Programming :

date manipulation

Quote Reply
date manipulation
I apologize if this has been covered in a past thread - although I have been unable to find any...

I am looking for a way to display ## days since <date>.
I would like to provide the script with the start date and have it calculate how many days it has been from then to the current date - but to display the information is DAYS only.

Any suggestions ?

Quote Reply
Re: date manipulation In reply to
have a look at the Date::Calc module available from cpan.perl.org. That does what you need

(or is it Date::Manip -one of the two, I forget which)

--mark

Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.
Quote Reply
Re: date manipulation In reply to
Thanks! I've downloaded both of them and am looking at the uses now. I'm a bit concerned, tho - I have limited access to perl on my ISP's Unix Server. Is this going to be a problem? Or can it be accessed similar to the CGI.pm, etc ?
What I really need is a stand-alone script to call (SSI) that will place the calculated date in my HTML. Get Date will work, but the easiest calculation I can come up with would be to have a mod that converted it to Julian, subtracted my start date (after Julian conversion) and displayed the difference.

Any additional suggestions appreciated!

Thanks,

Keien

Quote Reply
Re: date manipulation In reply to
In Reply To:
Is this going to be a problem?
Nope...

In Reply To:
Or can it be accessed similar to the CGI.pm, etc ?
If the modules are already installed in your server, then yes...codes have been provided in this forum and other forums in this site to get a report of installed Perl modules in your server.

In Reply To:
Get Date will work, but the easiest calculation I can come up with would be to have a mod that converted it to Julian, subtracted my start date (after Julian conversion) and displayed the difference
Read the documentation at http://www.cpan.org, buy a Perl book that covers modules, or read your local Perl docs in your account.


Regards,

Eliot Lee
Quote Reply
Re: date manipulation In reply to
Try this.
This will retrun the difference in days between todays date and some other date.

my $start_date = "31-Oct-2000";
my $today = &date_to_unix(&get_date);

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

$temp = int(((&date_to_unix($start_date) + ((&date_to_unix(&get_date)) * 86400)) - time())/86400);
$days = (($today) - ($temp));
print "Difference between start date and todays date in days - $days\n";

sub get_date {
# --------------------------------------------------------
# Returns the date in the format "dd-mmm-yyyy".
# Warning: If you change the default format, you must also modify the &date_to_unix
# subroutine below which converts your date format into a unix time in seconds for sorting
# purposes.
my ($time) = $_[0];
($time) or ($time = time());
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";
}

sub date_to_unix {
# --------------------------------------------------------
# This routine must take your date format and return the time a la UNIX time().
# Some things to be careful about..
# int your values just in case to remove spaces, etc.
# catch the fatal error timelocal will generate if you have a bad date..
# don't forget that the month is indexed from 0!
#
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);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}

There may be a better way to do it, but I've tried this and it works.

Bob
http://totallyfreeads.com


Quote Reply
Re: date manipulation In reply to
lanerj -

Works GREAT! Thanks for your help :)

keien

Quote Reply
Re: date manipulation In reply to
Hi Bob

Thank you for your tid bit of code. I have been looking for that answer on the web for a while and everyone directed me to some complex modules which bogged down my programs. Your tid-bit did the trick very well and it's so simple. 'doh!

Thank you

Wil

Quote Reply
Re: date manipulation In reply to
Glad I could help.

Bob
http://totallyfreeads.com

Quote Reply
Re: date manipulation In reply to
Mark:

I'm off on a new date math issue and remembered you mentioned the module "Date::Manip" in this thread.

I've downloaded and installed the module without fail. I've even managed to call it from DBMAN and get some output ... here's my problem.

I'm allowing a date entry field. I want to take the date entered and add 35 days to that date and place it as another field. This all works fine, except that my date entered is as : 01-JAN-2001 and the MANIP is giving me results as: 20010101 08:00:00

Have you worked with this module? Have any recommendations for removing the TIME portion and reformatting the date?

Here's what I have:
if ($in{'EPD'}) {
$in{'DPD'} = (&DateCalc($in{'EPD'},"+ 25 business days"));

}
and the "formatting stuff" that SHOULD produce the results I'm looking for:
$format = "%d-%b-%Y";
$string = &UnixDate($date,$format);

Any help would be greatly appreciated!

Thanks,

keienb