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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

localtime - 1 day

Status
Not open for further replies.

gorgor

Programmer
Aug 15, 2002
164
US
I'm trying to figure out how to get the date for 'yesterday'. I need it in the same format as this:

my ($sec,$min,$hour,$day,$month,$year) = localtime();
$month++; # Add 1 to the month
$year+=1900; # Add 1900 to the year
$day=sprintf("%02d",$day); # Pad the day with a 0
$month=sprintf("%02d",$month); # Pad the month with a 0

my $today = "$month$day$year";

I'm banging my head on a wall over this one. Sure, I could subtract 1 from $day, but what if today's date is 01/01/2003 or 03/01/2002. What about leap year, 31 day months vs 30 day months vs 28 day months? I just can't think of any way to do it and get it in the same format.

 
You can get yesterday by feeding localtime the current time less 24hrs worth of seconds.
Code:
my ($day,$mon,$year) = (localtime(time-86400))[3..5];
my $today = sprintf("%02d%02d%d", ++$mon, $day, $year+=1900)

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top