in reply to How do i get "the last day of last month"?
Or, if you only have core modules at your disposal, then you could work out the first of next month and subtract a day:
use strict; use Time::Local; use constant ONE_DAY => 24 * 60 * 60; my ($sec,$min,$hour,$mday,$mon,$year) = localtime(); $mday = 1; $mon = ($mon + 1) % 12; $year += 1 if($mon == 0); my $time = timelocal($sec,$min,$hour,$mday,$mon,$year) - ONE_DAY; print scalar localtime($time);
Update: Sorry, I completely misread the question and gave you the last day of this month. The last day of last month is a little easier:
use Time::Local; use constant ONE_DAY => 24 * 60 * 60; my ($sec,$min,$hour,$mday,$mon,$year) = gmtime(); $mday = 1; my $time = timegm($sec,$min,$hour,$mday,$mon,$year) - ONE_DAY; print "Date: " . localtime($time) . "\n"; $mday = (localtime($time))[3]; print "Day of month: $mday\n";
Update 2: As MarkM notes below, daylight savings changes could trip these snippets up. I originally coded them to set $sec, $min and $hour all to zero and then subtract say 4 hours - which I think should be pretty safe. I changed it before posting because I thought it was a bit obfuscated - but clear code that's wrong is no less wrong :-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: How do i get "the last day of last month"?
by MarkM (Curate) on Apr 28, 2003 at 03:46 UTC |