CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Dates

 



thangorn
stranger

Oct 10, 2001, 4:56 AM

Post #1 of 3 (811 views)
Dates Can't Post

Hello

I am trying to find a way of making 5 variables containing dates. variable 1 will be todays date and the the other 4 variables containing the previous dates.

the script will be run every day and i want the dates to change appropiatly. so variable 1 is always the current date. and variable 2 the day before etc etc.

Any help appreciated

Cheers



rGeoffrey
User / Moderator

Oct 11, 2001, 9:20 AM

Post #2 of 3 (797 views)
Re: Dates [In reply to] Can't Post

This will get you an array of the five dates starting with today...


Code
#!/usr/local/bin/perl 

use strict;

my $now = time;
my @dates;

for my $offset (0..4) {
push (@dates, &Show_Time ($now - ($offset * 60 * 60 * 24)));
print $dates[$offset], "\n";
}

sub Show_Time
{
my($when) = @_;

my @thetime = localtime($when);

my $result = sprintf ("% 02d-%s-% 04d",
($thetime[3],
('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')[$thetime[4]],
(1900 + $thetime[5])
));

return ($result);
}

You will need to remove the space between each '%' and the '0' in the sprintf. I had to add the space so it would display properly on this page.



thangorn
stranger

Oct 12, 2001, 8:36 AM

Post #3 of 3 (789 views)
Re: Dates [In reply to] Can't Post

Thanks rGeoffrey I solved this using Date::Calc that gave me $month $year $dayt variables i could play around with,, your way looks far more impresive though ;-)


Thx



 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives