I have the following loop to calculate the dates of the current week and print them out. It works, but I am swimming in the amount of date/time possibilities in Perl and want t
This should work:
use POSIX; # for strftime
my $time = time ();
my $seconds = 24*60*60;
my @time = gmtime ();
$time = $time - $time[6] * $seconds;
for my $wday (0..6) {
$time += $seconds;
my @wday = gmtime ($time);
print strftime ("%A %d %B %Y\n", @wday);
}
Gives me:
$ ./week.pl Monday 24 May 2010 Tuesday 25 May 2010 Wednesday 26 May 2010 Thursday 27 May 2010 Friday 28 May 2010 Saturday 29 May 2010 Sunday 30 May 2010
If you want to get weeks starting on Sunday, change $time[6]
to ($time[6] + 1)
.
This assumes you want the GMT weeks. Change gmtime
to localtime
to get local time zone weeks.