$datestamp - example: 20060608153125
$pdate - example: 06-08-2006
I then repeat the code to add $day - example 14
to get:
$edate - example: 06-22-2006
$edatestamp - example: 20060222
#!/usr/bin/perl# untitled4.pl
my $days = 14;
sub date_stamp {
my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
$year = $year + 1900;
$mon = ($mon + 1);
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10){$hour = "0$hour";}
if ($min < 10){$min = "0$min";}
if ($sec < 10){$sec = "0$sec";}
$datestamp = $year.$mon.$mday.$hour.$min.$sec;
$pdate = ("$mon\-$mday\-$year");
my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time+86400*$days);
$year = $year + 1900;
$mon = ($mon + 1);
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10){$hour = "0$hour";}
if ($min < 10){$min = "0$min";}
if ($sec < 10){$sec = "0$sec";}
$edatestamp = $year.$mon.$mday;
$edate = ("$mon\-$mday\-$year");
return $datestamp, $pdate, $edatestamp, $edate;
} #end date_stamp
This code works but if you know a better way of doing it please show me.
Have you considered using the POSIX [search.cpan.org] module? It includes quite a few nice functions to help you manipulate date and time. CPAN also has quite a few other Date modules [search.cpan.org] that may be of interest.
$now = time; # assigns the current value in epoch seconds to variable '$now'
Then to add four days to it, you just add four days worth of seconds:
$then = $now + 4*24*60*60;
Now to convert that into human-readable form...
($second, $minute, $hour, $day, $month, $year, $weekday, $dayOfYear, $IsDST) = localtime($then);
Note a few quirks about these values. First, the month range is 0-11, not 1-12, so you'll need to add 1 to the number of months. And the value for the year has 1900 subtracted from it. So the value for this year would be returned as 106, so you'd need to add 1900 to it.
Finally, if you have a date that you need to convert *to* epoch seconds, you do so like this:
use Time::Local;
$epoch = timelocal($sec, $min, $hours, $mday, $mon, $year);
You need to feed it Perl-time-style values. That is, feed it a $mon that's in the range 0-11, and a year that has 1900 subtracted from it.
Is all this awesome or what?