25/06/2003 - 25/07/2003
the way it needs to be calculated is depending on the current date. for example if today is the 26th of July then the period will be like the above example.
If today's date was 30/07/03 then the period that the script prints should be 29/06/2003 - 29/07/2003
any suggestions would be appreciated.
cheers
linda
$today = get_today();
$lastmonth = get_prev_month($today);
print "$lastmonth - $today\n"; sub get_today {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
return "$mday/$mon/$year";
}
sub get_prev_month {
my $date = shift;
my($pday, $pmon, $pyear) = split(/\//, $date);
if($pmon == 1) {
$pyear -= 1;
$pmon = 12;
} else {
$pmon -= 1;
}
return "$pday\/$pmon\/$pyear";
}
it seems to be doing fine but when i run it i get 27/5/2003 - 27/6/2003.
theres two problems with that. firstly its from month 05 to month 06 when the month now is 07 and therefore it should be from month 06 - 07
and secondly today is the 27th its showing from the 27th of the previous month to the 27th of this month. if today is 27th it should show the date from 26 of the previous month to the 26th of this month (yesterday)
any suggestions would be appreciated.
cheers
linda
sub get_prev_month {
my $date = shift;
my($pday, $pmon, $pyear) = split(/\//, $date);
if($pmon == 1) {
$pyear -= 1;
$pmon = 12;
} else {
$pmon -= 1;
$pday -= 1;
}
return "$pday\/$pmon\/$pyear";
}
but it seems to not work properly. it only seems to subtract 1 day from the previous month and not from the current month.
any suggestions would be appreciated.
cheers
linda
[perl]
#!E:\perl\bin\perl.exe
print "Content-type: text/html\n\n";
$pday = 1;
$pmon = 8;
$pyear = 2003;
if($pmon == 1) {
$pyear -= 1;
$pmon = 12;
}
else {
$pmon -= 1;
$pday -= 1;
}
if($pday == 0) {
$pmon -= 1; # substract another from $pmon
if($pmon == 1) {
$pyear -= 1;
$pmon = 12;
}
if ($pmon =~ /^(00)$/){$pday = 31; }
elsif ($pmon == "1"){$pday = 29; } # it's february
else {$pday = 30; }
}
print "$pday\/$pmon\/$pyear";
[/perl]
if the above works for you too, maybe there's something wrong with the format of $pmon when your sub routine recieves it. You can try printing the original $pmon to debug.
Sorry to complicate your problem but your sub routine does not consider $pday possibly being '1' ..then $pday should become either '30' or '31' .. hehe... it's a fun job we have no?
I added something to the above script to suggest a solution for that..february has 29 days right.. better check on how I determine the number of days in each month.. I did this by the 'counting knuckles" method ..
but er.. up to you to
consider february 28 days once every four years and stuff... ;)
<edit reason> edited to substract one more from $pmon if $pday =1 </edit reason>
<added>Related and containing a possible solution:
[webmasterworld.com...] </added>
Firstly "...only seems to subtract 1 day from the previous month and not from the current month..."
Yes, that is how you have written your code. Instead of modifying your 'get_prev_month() function, you could do:
$yesterday = get_today(-1);
$lastmonth = get_prev_month($yesterday);
print "$lastmonth - $yesterday\n";
And then modify your 'get_today' function to take a parameter being a number of days offset. When applying the offset, take note of 28 vs 30 vs 31 days and leapyears, as already indicated by Damian
Secondly"...it should be from month 06 - 07
You could add 1 to $mon.
You can either write:
$mon = $mon + 1;
or the notation with which your script adds 1900 to $year:
$mon += 1; ..."
I'd suggest that you must add one to the month. For some reason (I am yet to be convinced it is a good one), months go from 0 to 11 instead of 1 to 12. Same format for Javascript. Generally when adding one, the notation I'd like to see used $mon++;
Thirdly
It may be an idea to separate the date formating from the date manipulation. If you use this code for different countries, you will need different formats. It would be nice to just change the function which changes the printing format, without having to change any of the other functions which manipulate the date. What I am suggesting is to have the input and output of get_prev_month(), and the output of get_today(), be of the format returned by perl's time function rather than what your get_today() currently returns. Sorry to complicate things. Just a suggestion; take it or leave it. ;)
Shawn