Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get a date which is less than a month

Status
Not open for further replies.

thendal

Programmer
Aug 23, 2000
284
0
0
Hi all!

How can i get a date which less than a month from today's date.
todays date 2002-1-2(yyyy/mm/dd)
less than a month 2001-12-2(yyyy/mm/dd)


Thanks.


 
I'm not sure if this is what you're asking, but if you'd like to test to loop the month/year backwards, try this:

#! /usr/bin/perl

use Time::Localtime;

# getting the values of LocalTime

($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = localtime;
$year += 1900; #years start at 1900 as a value of 0 so add 1900 to get 4 digit year
$month += 1; #localtime has months from 0 to 11, so add one for current month

$lastmonth = $month - 1;

if ($lastmonth == 0)
{
$lastmonth = 12;
$lastyear = $year - 1;
}
else
{
$lastyear = $year;
}

# alter month and day to hold a 2 digit number at all times
if($month <= 9){$month = &quot;0$month&quot;};
if($mday <= 9){$mday = &quot;0$mday&quot;};

print &quot;Current date is: $year/$month/$mday\n&quot;;
print &quot;Last month date is: $lastyear/$lastmonth/$mday\n&quot;;

If you're curious to know what all of the Localtime variables that I used are, they're listed below.

$sec = Current Seconds
$min = Current Minutes
$hour = Current Hour
$mday = Current Day of Month
$month = Current Month (Values are 0 - 11)
$year = Current Year(Values start at 0 for 1900 and go up from there)
$wday = Current Week Day (Values are 0 - 6)
$yday = Current Day of Year
$isdst = Is Daylight Savings Time (returns 0 or 1)
 
Thanks rieekan thats what i am expecting ..
sorry for the delayed response ..my computer got screwed up..
Thanks again.
 
Incidentally the following:

if($month <= 9){$month = &quot;0$month&quot;};
if($mday <= 9){$mday = &quot;0$mday&quot;};

can be shortened to:

$month =~ s/^(\d)$/0$1/;
$mday =~ s/^(\d)$/0$1/;

as this will actually use less processing. But as Larry says, TIMTOWTDI :)

Barbie. Leader of Birmingham Perl Mongers
 
How would I get listing of files(based on modification date) for the last month only?
 
$full_path='specify the directory here';
chdir($full_path);
opendir (DIR, $full_path) || &cgiError (&quot;Error Reading Directory:&quot;, &quot;$!&quot;);
@allfiles = readdir(DIR);
closedir (DIR);

for ($x=0; $x<=$#allfiles; $x++) {
if (-d $allfiles[$x])
{
push(@directories,&quot;$allfiles[$x]&quot;);
}
else { push(@files,&quot;$allfiles[$x]&quot;); }
}

@directories = sort { lc($a) cmp lc($b) } @directories;
$totalnum = $#directories; $totalnum++;

for($i=1; $i<$totalnum; $i++) {
$temp_file = $directories[$i];
($filesize, $filedate, $fileperm) = (stat($temp_file))[7,9,2];
@months = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
($min, $hr, $day, $mon, $yr) = (localtime($filedate))[1,2,3,4,5];
$yr=1900+$yr;
$filedate = &quot;$day-$months[$mon]-$yr&quot;;
print &quot;Directory:$directories[$i],Date:$filedate\n&quot;;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top